Export (0) Print
Expand All

Decimal Structure

Represents a decimal number.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public value class 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>
JScript suports the use of structures, but not the declaration of new ones.

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 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 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.

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>
   public ref class PiggyBank
   {
   protected:
      Decimal MyFortune;

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

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

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

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

      virtual System::String^ ToString() override
      {
         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;

    public void AddPenny()
    {
        myFortune = Decimal.Add(myFortune, System.Convert.ToDecimal(0.01));
    } //AddPenny

    /** @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 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";
    }
}

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

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft