Money data type for .NET?

2019-03-08 13:39发布

Looking for a good Money data type for .NET that supports currencies and exchange rates (with related behaviour & operations).

  • Note: I started searching for the source code seen in print in the book Test-Driven Development By Example by author Kent Beck - he develops a nice Monetary concept. Unable to find the complete source online. The book does not contain one singular listing - instead it develops the source code over the duration of the book.

Although the book doesn't go deeper I would also like the Money class to support different rounding mechanisms because that also varies among financial institutions around the globe.

Edit 1: Clarifications

By "Money class" I mean a .NET data type that I can use to support money and its operations, complete with currency support and exchange rate calculations. Something like that is a higher level abstraction and might internally use a primitive data type. Also it might rely on a bundle of classes like: Money, ExchangeRate, Bank, etc.

It might be a value type (i.e. struct) or reference type (i.e. class) but either way it would be complete and support the aforementioned features.

Edit 2: Objectives of Money data type

(This also shows why a raw decimal, int or other primitive type won't suffice for all Money needs)

Objectives of a Money data type would be:

  • Safety features (i.e. prevent arithmetic on different currency types).
  • Store the currency type with the value (i.e. AUD, US, DKK).
  • Store formatting details (i.e. decimal grouping, currency symbols, etc.).
  • Conversion providers (e.g. exchange rate) to help round out the solution.
  • Reduce multiple currencies in an expression to achieve a result.

Note: Varying data values like exchange rates can be loaded from an external source and used. This question does not infer anything dynamic is hard-coded into the concept of Money.

8条回答
Evening l夕情丶
2楼-- · 2019-03-08 14:16

If you are looking for patterns, you could check out Joda Money. It is Java, but should give you some ideas on an API. A C# implementation would be much less verbose due to operator overloading.

查看更多
我命由我不由天
3楼-- · 2019-03-08 14:18

you will probably find that creating your own class will result in the best solution.

查看更多
Luminary・发光体
4楼-- · 2019-03-08 14:22

NodaMoney provides a library that treats Money as a first class citizen in .NET and handles all the ugly bits like currencies and formatting.

It complies with the currencies in ISO 4217. And it's the .NET counterpart of the java library JodaMoney.

查看更多
男人必须洒脱
5楼-- · 2019-03-08 14:29

Money Data Type @ The Code Project

http://www.codeproject.com/KB/vb/moneyDatatype.aspx

Author states similar problem:

as part of a recent application I realized how lacking .NET is for currency support, don't get me wrong, there are many "pieces" but the glue for all items is missing, so this article is a response to that.

and fulfills objectives

my main objectives became

  • Store the currency type with the value (i.e. AUD, US, DKK).
  • Store formatting details (i.e. decimal grouping, currency symbols, etc.).
  • Conversion providers, I didn't want to hard code this as it is a datatype and not a solution.
  • Development safety features (i.e. prevent arithmetic on different currency types).

So far this the closest .NET code to what I'm searching for. It fulfills most requirements of Money.

If anybody has something better it would be much appreciated.

查看更多
家丑人穷心不美
6楼-- · 2019-03-08 14:33

Martin Fowler considers money as a special case of "Quantity", secondly he thinks the right Data Type for money should be the Big Integer. And he does have a point.

Quantity and Money Pattern by Martin Fowler

查看更多
你好瞎i
7楼-- · 2019-03-08 14:36

Have a look here:

http://blogs.msdn.com/lucabol/archive/2008/12/04/financial-functions-for-net-released.aspx

It provides a .NET library replicating all the excel financial functions.

Doing currency conversion is tricky, because obviously it changes continously, so hardcoded values will be more or less useless. However, you may be able to use a web service to access up-to-date exchange rates. This one looks like a good start. Even better, a REST-style interface to the same converter:

http://www.webservicex.com/CurrencyConvertor.asmx/ConversionRate?FromCurrency=GBP&ToCurrency=EUR

So that outputs the conversion rate of pounds sterling to euros.

查看更多
登录 后发表回答