Skip to content
Tutorials Center
My learning

Topics

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.

C#
decimal price = 19.99m;
decimal total = price * 3;

Try it

C# Exact arithmetic

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.

C# Scale, rounding and conversion

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

C#
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.5m and 1.50m compare equal but print differently, because a decimal remembers how many digits follow the point. Multiplying 1.10m * 2.00m gives 2.2000m. Format with ToString("F2") or "C" when you need a fixed number of places.
  • Banker's rounding by default. Math.Round(2.5m) is 2; pass MidpointRounding.AwayFromZero for the schoolbook rule.
  • Not an exact fraction. 1m / 3 * 3 is 0.9999999999999999999999999999m, not 1. Decimal fixes decimal fractions, not all fractions.
  • Money advice. Store currency amounts as decimal (or integer cents) together with a currency code; never double. Round only at the end of a calculation, using a documented rule.
  • Performance. Arithmetic is roughly an order of magnitude slower than double and 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.Json writes it as a JSON number with all digits, but JavaScript readers will lose precision beyond about 15 digits.