C# 有谁知道.NET 中的货币类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/274296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:07:43  来源:igfitidea点击:

Does anyone know of a money type in .NET?

c#.netcurrency

提问by Mateo

Does anyone know of an already implemented money type for the .NET framework that supports i18n (currencies, formatting, etc)? I have been looking for a well implemented type and can't seem to find one.

有谁知道支持 i18n(货币、格式等)的 .NET 框架已经实现的货币类型?我一直在寻找一种实现良好的类型,但似乎找不到。

采纳答案by jfs

Check this article A Money type for the CLR

检查这篇文章CLR 的货币类型

A convenient, high-performance money structure for the CLR which handles arithmetic operations, currency types, formatting, and careful distribution and rounding without loss.

一种方便、高性能的 CLR 货币结构,可处理算术运算、货币类型、格式以及仔细的分布和四舍五入而不会丢失。

回答by Jason Hymanson

I think you want to use the decimal data type, and use the appropriate overload for ToString().

我认为您想使用十进制数据类型,并为ToString()使用适当的重载。

CultureInfo current  = CultureInfo.CurrentCulture;
decimal myMoney = 99.99m;

//formats as money in current culture, like .99
string formattedMoney = myMoney.ToString("C", current); 

回答by Chii

i would use integer/long, and use a very low denomination like cents (or pence) - then there would be no decimal to work with, and all calculations can be rounded to the nearest cent.

我会使用整数/长整数,并使用非常低的面额,如美分(或便士)——这样就没有小数可以使用,所有计算都可以四舍五入到最接近的美分。

or, take a look at Martin Fowler's book "Patterns of Enterprise Application Architecture". In that book, he talked about how to implement a money class. http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420

或者,看看 Martin Fowler 的书“企业应用程序架构模式”。在那本书中,他谈到了如何实现货币课程。http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420