Does anyone know of a money type in .NET?

扶醉桌前 提交于 2019-11-27 17:22:33

问题


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.


回答1:


Check this article A Money type for the CLR

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




回答2:


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

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

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



回答3:


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



来源:https://stackoverflow.com/questions/274296/does-anyone-know-of-a-money-type-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!