What data type should I use to represent money in C#?

Use decimal and money in the DB if you're using SQL.


Decimal is the one you want.


Use 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.

Neither System.Single (float) nor System.Double (double) are precise enough capable of representing high-precision floating point numbers without rounding errors.


In C#, the Decimal type actually a struct with overloaded functions for all math and comparison operations in base 10, so it will have less significant rounding errors. A float (and double), on the other hand is akin to scientific notation in binary. As a result, Decimal types are more accurate when you know the precision you need.

Run this to see the difference in the accuracy of the 2:

using System;
using System.Collections.Generic;
using System.Text;

namespace FloatVsDecimal
{
    class Program
    {
        static void Main(string[] args) 
        {
            Decimal _decimal = 1.0m;
            float _float = 1.0f;
            for (int _i = 0; _i < 5; _i++)
            {
                Console.WriteLine("float: {0}, decimal: {1}", 
                                _float.ToString("e10"), 
                                _decimal.ToString("e10"));
                _decimal += 0.1m;
                _float += 0.1f;
            }
            Console.ReadKey();
        }
    }
}

Tags:

C#