Money data type for .NET?
Asked Answered
M

8

35

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.

Must answered 7/2, 2010 at 2:0 Comment(10)
Not sure what are you looking for exactly. Could you please explain more on "Money" class or any reference to identical class in other langs?Thermometer
Added an Edit to the question - "Money class" means "Money data type" in .NET. Maybe a C# class or a VB.NET Class, or similar structure.Must
Normally, decimal is always used when dealing with money/currency to avoid rounding problems caused from floating-point data types. I still can't see solid use-cases of Money Class you are after other than currency conversion, exchanges specific rounding support, .. the first of which can be done with Web Service as suggested by @Kazar.Thermometer
@m3rLinEz: Use Case is any application transacting money. Asking for a dedicated data type in the spirit of encapsulation and abstraction for use in many different apps that need money and its operations in a global context. Also to reap data type benefits like maintainability, reusability, testability, etc. Looking for a higher level abstraction than you might be considering- of course web services can ultimately be used to acquire exchange rate info, but first I want the money abstraction to support those.Must
The trouble I see with the idea of a "Money" class is the same trouble we have with time zones: The rules change all the time. Actually, it's worse than that - at least with date/time data we have a reference time zone, UTC, whereas currencies all float against each other. All of the external dependencies associated with a unit of currency make it a poor choice for encapsulation; truly, a currency is simply a fixed-point decimal with a region code attached; any functionality more complicated than this would be unreliable at best.Lorineloriner
@Aaronaught: There are other benefits like protection against multiplying two amounts having different currencies. That is just one.Must
https://mcmap.net/q/450536/-does-anyone-know-of-a-money-type-in-netGare
moneytype.codeplex.comGare
michaelbrumm.com/money.htmlGare
@Mauricio: Thanks, great links. There's momentum out there around CLR/Money.Must
D
9

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

Digamy answered 7/2, 2010 at 10:45 Comment(4)
Fowler uses an integer type to represent total cents in the money amount instead of a decimal type to represent dollar value with remaining cents.Must
I wrote a log parser for an MMO and it kept track of all the coinage that you looted throughout your gameplay. There were several denominations of coins but in the end it was easier to keep track of all money looted in terms of the lowest valued coin (copper). It makes performing operations on that money relatively easy and you don't have to worry about rounding errors.Repentant
Considering money as integers e.g. decimal 3.58 as int 358 cents is good if you don't need to deal with strict financial information that requires fractions of cents to be maintained (e.g. money markets, trading, etc). Money int quantity is good for simple money needs like retail.Must
This is right and the integer problem is also easily solved. I have developed a custom Number class of unlimited length which is like BigInteger but with decimal places and full mathematical capabilities. I agree no special money class is needed, just business logic and formatting helpers, e.g. Of course you still need to store the name of the currency somewhere in the container class/struct (e.g. business object, "order"). It's the wrong design to try and store all the information about the currency in every amount, even just the currency name, because it's a total waste of memory/load.Stumpy
G
7

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.

Giulia answered 7/2, 2010 at 2:0 Comment(0)
M
5

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.

Must answered 7/2, 2010 at 4:46 Comment(1)
This looks good. And it shed some lights to me what operations should Money class has.Thermometer
G
2

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.

Grader answered 7/2, 2010 at 4:53 Comment(1)
This is nice because Java syntax is relatively easy to port to C#, and "borrowing" concepts is useful as suggested.Must
C
1

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.

Chop answered 7/2, 2010 at 2:0 Comment(0)
G
1

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.

Gingras answered 7/2, 2010 at 2:16 Comment(1)
That's interesting and it may contain some useful financial calculations however I'm searching for a standalone money class to use as a data type. I scanned the source code of the financial library and cannot find such a thing, only functions. Of course maybe it's because I don't understand F# well enough - let me know if the class is actually in there.Must
P
1

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

Protomartyr answered 7/2, 2010 at 4:11 Comment(1)
I'm surprised because my initial reaction was: there's likely a plethora of options out there for such as thing. If no answers provide a suitable premade solution then I might have to do as you say. In fact have already started for the heck of it.Must
T
0

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 be Open, 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.

Thermometer answered 7/2, 2010 at 4:47 Comment(1)
That's an impressive library and it does support a Money data type although .NET isn't directly supported. The FAQ says: "7.2. Does QuantLib support .NET? Not directly. C# bindings for QuantLib are available, though; see the extensions page." -- quantlib.org/faq.shtml Of course there are ways to kludge it to .NET, but personally I'm unsure of this SWIG binding mechanism it suggests to make it work swig.sourceforge.netMust

© 2022 - 2024 — McMap. All rights reserved.