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

What is the best data type to use for money in 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;

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.

Id like to point to this excellent answer by zneak on why double shouldnt be used.

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

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

Leave a Reply

Your email address will not be published.