The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Decimal Structure

.NET Framework 3.0
Represents a decimal number.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

```[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Decimal : IFormattable, IComparable, IConvertible,
IComparable<decimal>, IEquatable<decimal>
```
```/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public final class Decimal extends ValueType implements IFormattable, IComparable,
IConvertible, IComparable<Decimal>, IEquatable<Decimal>
```
```Not applicable.
```

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

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

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 is of the form, ((-296 to 296) / 10(0 to 28)), where -296-1 is equal to MinValue, and 296-1 is equal to MaxValue.

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

Conversion Considerations

This type provides methods that convert Decimal values to and from SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, and UInt64. 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 is thrown.

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

Conversions from Single or Double to Decimal throw an OverflowException if the result of the conversion cannot be represented as a Decimal.

Implemented Interfaces

This type implements interfaces IComparable, IComparable, IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.

The following code example demonstrates the use of Decimal.

```/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank {
protected decimal MyFortune;

}

public decimal Capacity {
get {
return Decimal.MaxValue;
}
}

public decimal Dollars {
get {
return Decimal.Floor(MyFortune);
}
}

public decimal Cents {
get {
return Decimal.Subtract(MyFortune, Decimal.Floor(MyFortune));
}
}

public override string ToString() {
return MyFortune.ToString("C")+" in piggy bank";
}
}

```
```/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank
{
protected System.Decimal myFortune;

{

/** @property
*/
public System.Decimal get_Capacity()
{
return Decimal.MaxValue;
} //get_Capacity

/** @property
*/
public System.Decimal get_Dollars()
{
return Decimal.Floor(myFortune);
} //get_Dollars

/** @property
*/
public System.Decimal get_Cents()
{
return Decimal.Subtract(myFortune, Decimal.Floor(myFortune));
} //get_Cents

public String ToString()
{
return myFortune.ToString("C") + " in piggy bank";
} //ToString
} //PiggyBank

```
```/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank {
protected var MyFortune : Decimal;

}

public function get Capacity() : Decimal {
return Decimal.MaxValue;
}

public function get Dollars() : Decimal {
return Decimal.Floor(MyFortune);
}

public function get Cents() : Decimal {
return Decimal.Subtract(MyFortune, Decimal.Floor(MyFortune));
}

public function ToString() : String {
return MyFortune.ToString("C")+" in piggy bank";
}
}

```

All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

Caution:

Assigning an instance of this type is not thread safe on all hardware platforms because the binary representation of that instance might be too large to assign in a single atomic operation.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0