What is the best data type to use for money in C#?

C#TypesCurrency

C# Problem Overview


What is the best data type to use for money in C#?

C# Solutions


Solution 1 - C#

As it is described at decimal as:

> The decimal keyword indicates a 128-bit data type. Compared to > floating-point types, the decimal type has more precision and a > smaller range, which makes it appropriate for financial and monetary > calculations.

You can use a decimal as follows:

decimal myMoney = 300.5m;

Solution 2 - C#

System.Decimal

> The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding.

I'd like to point to this excellent answer by zneak on why double shouldn't be used.

Solution 3 - C#

Use the Money pattern from [Patterns of Enterprise Application Architecture][2]. specify amount as decimal and the currency as an enum.

[2]: http://martinfowler.com/books.html#eaa "Patterns of Enterprise Application Architecture"

Solution 4 - C#

Decimal. If you choose double you're leaving yourself open to rounding errors

Solution 5 - C#

decimal has a smaller range, but greater precision - so you don't lose all those pennies over time!

Full details here:

http://msdn.microsoft.com/en-us/library/364x0z75.aspx

Solution 6 - C#

Agree with the Money pattern: Handling currencies is just too cumbersome when you use decimals.

If you create a Currency-class, you can then put all the logic relating to money there, including a correct ToString()-method, more control of parsing values and better control of divisions.

Also, with a Currency class, there is no chance of unintentionally mixing money up with other data.

Solution 7 - C#

Another option (especially if you're rolling you own class) is to use an int or a int64, and designate the lower four digits (or possibly even 2) as "right of the decimal point". So "on the edges" you'll need some "* 10000" on the way in and some "/ 10000" on the way out. This is the storage mechanism used by Microsoft's SQL Server, see http://msdn.microsoft.com/en-au/library/ms179882.aspx

The nicity of this is that all your summation can be done using (fast) integer arithmetic.

Solution 8 - C#

Most applications I've worked with use decimal to represent money. This is based on the assumption that the application will never be concerned with more than one currency.

This assumption may be based on another assumption, that the application will never be used in other countries with different currencies. I've seen cases where that proved to be false.

Now that assumption is being challenged in a new way: New currencies such as Bitcoin are becoming more common, and they aren't specific to any country. It's not unrealistic that an application used in just one country may still need to support multiple currencies.

Some people will say that creating or even using a type just for money is "gold plating," or adding extra complexity beyond the known requirements. I strongly disagree. The more ubiquitous a concept is within your domain, the more important it is to make a reasonable effort to use the correct abstraction up front. If you want to see complexity, try working in an application that used to use decimal and now there's an additional Currency property next to every decimal property.

If you use the wrong abstraction up front, replacing it later will be a hundred times more work. That means potentially introducing defects into existing code, and the best part is that those defects will likely involve amounts of money, transactions with money, or just anything with money.

And it's not that difficult to use something other than decimal. Google "nuget money type" and you'll see that numerous developers have created such abstractions (including me.) It's easy. It's as easy as using DateTime instead of storing a date in a string.

Solution 9 - C#

Create your own class. This seems odd, but a .Net type is inadequate to cover different currencies.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNotDanView Question on Stackoverflow
Solution 1 - C#Lee TreveilView Answer on Stackoverflow
Solution 2 - C#David WalschotsView Answer on Stackoverflow
Solution 3 - C#lmsasuView Answer on Stackoverflow
Solution 4 - C#SquidScareMeView Answer on Stackoverflow
Solution 5 - C#dommerView Answer on Stackoverflow
Solution 6 - C#LennaertView Answer on Stackoverflow
Solution 7 - C#dszView Answer on Stackoverflow
Solution 8 - C#Scott HannenView Answer on Stackoverflow
Solution 9 - C#Noel KennedyView Answer on Stackoverflow