The decimal
type is a 128-bit data type suitable for financial and monetary calculations. The decimal
type can represent values ranging from 1.0 × 10-28 to approximately 7.9 × 1028 with 28-29 significant digits.
The finite set of values of type decimal
are of the form s × m × 10e, where s is 1 or –1, 0 = m < 296, and -28 = e = 0. The decimal type does not support signed zeros, infinities, and NaN's.
A decimal
is represented as a 96-bit integer scaled by a power of ten. For decimal
s with an absolute value less than 1.0m
, the value is exact to the 28th decimal place, but no further. For decimal
s with an absolute value greater than or equal to 1.0m
, the value is exact to 28 or 29 digits. Contrary to the float
and double
data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal
representation. In the float
and double
representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.
If one of the operands of a binary operator is of type decimal
, then the other operand must be of an integral type or of type decimal
. If an integral type operand is present, it is converted to decimal
before the operation is performed.
Operations on values of type decimal are exact to 28 or 29 digits, but to no more than 28 decimal places. Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position.
If a decimal arithmetic operation produces a value that is too small for the decimal format after rounding, the result of the operation becomes zero. If a decimal
arithmetic operation produces a result that is too large for the decimal
format, an OverflowException
is thrown.
The decimal
type has greater precision but smaller range than the floating-point types. Thus, conversions from the floating-point types to decimal
might produce overflow exceptions, and conversions from decimal
to the floating-point types might cause loss of precision. For these reasons, no implicit conversions exist between the floating-point types and decimal
, and without explicit casts, it is not possible to mix floating-point and decimal
operands in the same expression.