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.
I am the author of NMoneys which I think it might come handly. It has not been "officially released" yet, but it will change very little until it does.
I do understand your points about the benefit of having abstraction layer over money here. However, my view on money and its "operations" is quite blurry. For other things like
File
, it's clear to me there should beOpen
,Read
,Write
,Close
operations. But for Money, I can't think of much other than basic math operations (+,-,*,/)One of C++ quantitative finance lib I know does contain this Money abstraction (http://quantlib.org/reference/class_quant_lib_1_1_money.html.) But you can clearly see that this is a very thin wrapper which provide basic operator overloads and unit conversion over Decimal.
In most cases, I think decimal can fulfill your requirement. If there is specific Money operation you need to support, I think it's okay to roll out your own classes.