System.Decimal Structure

Represents a decimal number.

See Also: Decimal Members


public struct Decimal : IComparable, IComparable<decimal>, IConvertible, IEquatable<decimal>, IFormattable, System.Runtime.Serialization.IDeserializationCallback


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 that require 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. For example, the following code produces a result of 0.9999999999999999999999999999 instead of 1.

code reference: System.Decimal.Class#1

When the result of the division and multiplication is passed to the Math.Round(decimal, int) method, the result suffers no loss of precision, as the following code shows.

code reference: System.Decimal.Class#2

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a decimal value the form, ((-2 to 2) / 10), where -(2-1) is equal to decimal.MinValue, and 2-1 is equal to decimal.MaxValue. For more information about the binary representation of decimal values and an example, see the decimal.#ctor(Int32[]) constructor and the decimal.GetBits(decimal) method.

The scaling factor also preserves any trailing zeros in a decimal number. Trailing zeros do not affect the value of a decimal number in arithmetic or comparison operations. However, trailing zeros might be revealed by the erload:System.Decimal.ToString method if an appropriate format string is applied.

Conversion Considerations

This type provides methods that convert decimal values to and from sbyte, short, int, long, byte, ushort, uint, and ulong values. Conversions from these integral types to decimal are widening conversions that never lose information or throw exceptions.

Conversions from decimal to any of the integral types are narrowing conversions that round the decimal value to the nearest integer value toward zero. Some languages, such as C#, also support the conversion of decimal values to char values. If the result of these conversions cannot be represented in the destination type, an OverflowException exception is thrown.

The decimal type also provides methods that convert decimal values to and from float and double values. Conversions from decimal to float or double are narrowing conversions that might lose precision but not information about the magnitude of the converted value. The conversion does not throw an exception.

Conversions from float or double to decimal throw an OverflowException exception if the result of the conversion cannot be represented as a decimal.

Performing Operations on Decimal Values

The decimal type supports standard mathematical operations such as addition, subtraction, division, multiplication, and unary negation. You can also work directly with the binary representation of a decimal value by calling the decimal.GetBits(decimal) method.

To compare two decimal values, you can use the standard numeric comparison operators, or you can call the decimal.CompareTo(decimal) or decimal.Equals(decimal) method.

You can also call the members of the Math class to perform a wide range of numeric operations, including getting the absolute value of a number, determining the maximum or minimum value of two decimal values, getting the sign of a number, and rounding a number.

Thread Safety

This type is safe for multithreaded operations.


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,