C# Reference ยท Types and Literals
C# decimal type
Reference for the C# decimal type: 28-29 digit precision, the m suffix, scale and trailing zeros, rounding, overflow, and when to prefer it to double.
decimal (System.Decimal) is a 128-bit base-10 number type with 28 to 29 significant digits. Because it stores digits in decimal rather than binary, values such as 0.1 and 19.99 are represented exactly, and sums of prices add up the way they do on paper. That makes it the standard choice for money, tax and other quantities people check by hand.
A decimal is much slower than double (it is implemented in software) and has a smaller range, about ยฑ7.9 x 10^28. It does not have infinity or NaN. Literals need the suffix m or M; a plain 1.5 is a double and does not convert implicitly.
decimal price = 19.99m; decimal total = price * 3;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
decimal d = 12.345m; // System.Decimal decimal c = 1_000_000.00m; decimal z = decimal.Zero; decimal r = decimal.Round(d, 2, MidpointRounding.ToEven);
| Member | Description |
|---|---|
MinValue, MaxValue |
About ยฑ79,228,162,514,264,337,593,543,950,335. |
Zero, One, MinusOne |
Common constants. |
Round, Truncate, Floor, Ceiling |
Rounding helpers. Math.Round has the same overloads. |
Parse, TryParse |
Text to decimal. Use CultureInfo.InvariantCulture for fixed formats. |
GetBits |
The four 32-bit integers (low, mid, high, flags) that make up the value. |
Add, Subtract, Multiply, Divide, Remainder |
Method forms of the operators. |
Conversions. Every integer type converts implicitly to decimal. Conversions between decimal and float or double are explicit in both directions, because the binary value is rounded to about 15 digits on the way in.
Errors / undefined behavior. Out-of-range results throw OverflowException, even outside a checked block. Division by zero throws DivideByZeroException. Results that need more than 28 digits are rounded, never thrown.
Usage notes
- Scale is part of the value.
1.5mand1.50mcompare equal but print differently, because adecimalremembers how many digits follow the point. Multiplying1.10m * 2.00mgives2.2000m. Format withToString("F2")or"C"when you need a fixed number of places. - Banker's rounding by default.
Math.Round(2.5m)is2; passMidpointRounding.AwayFromZerofor the schoolbook rule. - Not an exact fraction.
1m / 3 * 3is0.9999999999999999999999999999m, not1. Decimal fixes decimal fractions, not all fractions. - Money advice. Store currency amounts as
decimal(or integer cents) together with a currency code; neverdouble. Round only at the end of a calculation, using a documented rule. - Performance. Arithmetic is roughly an order of magnitude slower than
doubleand values are 16 bytes. Do not use it for graphics or simulations. - No NaN. There is no way to represent "not a number"; use
decimal?for missing values. - Serialization.
System.Text.Jsonwrites it as a JSON number with all digits, but JavaScript readers will lose precision beyond about 15 digits.