Information
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

Represents a decimal number.

For a list of all members of this type, see Decimal Members.

System.Object
   System.ValueType
      System.Decimal

[Visual Basic]
<Serializable>
Public Structure Decimal
   Implements IFormattable, IComparable, IConvertible
[C#]
[Serializable]
public struct Decimal : IFormattable, IComparable, IConvertible
[C++]
[Serializable]
public __value struct Decimal : public IFormattable, IComparable,
   IConvertible

[JScript] In JScript, you can use the structures in the .NET Framework, but you cannot define your own.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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.

A decimal number is a signed, fixed-point value consisting of an integral part and an optional fractional part. The integral and fractional parts consist of a series of digits that range from zero to nine (0 to 9), separated by a decimal point symbol.

The binary representation of an instance of Decimal 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 2 96)/ 10 (0 to 28)), where -2 96 is equal to MinValue, and 296 is equal to MaxValue.

This type provides methods that convert Decimal values to and from type Char, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, and UInt64. Conversions from other types to Decimal are widening conversions that never lose information or throw exceptions.

Conversions from Decimal to other types are narrowing conversions that round the Decimal value to the nearest integer value toward zero. If the result of the conversion is not representable in the destination type, an OverflowException is thrown.

This type 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 is not representable as a Decimal.

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

Example

The following code sample demonstrates the use of Decimal:

[Visual Basic] 
' Keeping my fortune in Decimals to avoid the round-off errors.
Class PiggyBank
    Protected MyFortune As Decimal

    Public Sub AddPenny()
        MyFortune = [Decimal].Add(MyFortune, 0.01D)
    End Sub

    Public ReadOnly Property Capacity() As Decimal
        Get
            Return [Decimal].MaxValue
        End Get
    End Property

    Public ReadOnly Property Dollars() As Decimal
        Get
            Return [Decimal].Floor(MyFortune)
        End Get
    End Property

    Public ReadOnly Property Cents() As Decimal
        Get
            Return [Decimal].Subtract(MyFortune, [Decimal].Floor(MyFortune))
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return MyFortune.ToString("C") + " in piggy bank"
    End Function
End Class

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

    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    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";
    }
}

[C++] 
/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
public __gc class PiggyBank {
protected:
    Decimal MyFortune;

public:
    void AddPenny() {
        MyFortune = System::Decimal::Add(MyFortune, .01);
    }

    System::Decimal Capacity() {
        return MyFortune.MaxValue;
    }

    Decimal Dollars() {
        return Decimal::Floor(MyFortune);
    }

    Decimal Cents() {
        return Decimal::Subtract(MyFortune, Decimal::Floor(MyFortune));
    }

    System::String* ToString() {
        return String::Concat(MyFortune.ToString("C"),S" in piggy bank");
    }
};

}

using namespace Snippets;

void main (void) {
PiggyBank* pb = new PiggyBank();

for(Int32 i=0; i<378; i++) {
    pb->AddPenny();
}

Console::WriteLine(pb);
}

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

    public function AddPenny() {
        MyFortune = Decimal.Add(MyFortune, 0.01);
    }

    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";
    }
}

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Decimal Members | System Namespace | SByte | Int16 | Int32 | Int64 | Byte | UInt16 | UInt32 | UInt64 | Single | Double | Char

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft