Project Description

This Money class gives you the ability to work with money of multiple currencies as if it were built-in C# number types. It looks and behaves like a decimal.

  • Light: Only a 64bit double and 16bit int are used to persist an instance.
  • Fast: Access to the internal double value for fast calculations.
  • Easy: Overloaded operators - Use it like a decimal.
  • Safe: Don't worry about rounding errors - Money class handles it for you.
  • Currency codes are used to get everything from the MS Globalization classes. All look-ups happen from a singleton dictionary.
  • Formatting and significant digits are automatically handled.
  • The allocation function allows even distribution of Money.
  • Compatible with Entity Framework Code First classes. (NO extra coding required. Use it like a decimal)
  • SimpleMoney class also included for projects with a single currency. Even lighter, faster and simpler to use.

Decimals and doubles

Although the .Amount property wraps the class as Decimal, this Money class uses double to store the Money value internally. Only 15 decimal digits of accuracy are guaranteed! (16 if the first digit is smaller than 9)
It should be fairly simple to replace the internal double with a decimal if this is not sufficient and performance is not an issue. But decimal operations are MUCH slower than double (average of about 15x)

Use the .InternalAmount property to get to the double member. All the Money comparison operators use the Decimal wrapper with significant digits for the currency. All the Money arithmatic (+-/*) operators use the internal double value.

Important!

There is absolutely no guarantee for this class's functionality or performance. Although I spent some time to try and assure the code is bug-free, it has by no means been properly tested for commercial or enterprise use.

References

Martin Fowler patterns.
Making Money with C#: A Money type for the CLR
A few other articles on the web around representing money types
http://en.wikipedia.org/wiki/ISO_4217
http://www.currency-iso.org/iso_index/iso_tables/iso_tables_a1.htm

Last edited Sep 18, 2012 at 7:08 AM by k33p3rpb, version 11