5 out of 6 rated this helpful - Rate this topic

Decimal Structure

Represents a decimal number.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Decimal : IFormattable, IComparable, 
	IConvertible, IDeserializationCallback, IComparable<decimal>, IEquatable<decimal>

The Decimal type exposes the following members.

  Name Description
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Double) Initializes a new instance of Decimal to the value of the specified double-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Int32) Initializes a new instance of Decimal to the value of the specified 32-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Int32[]) Initializes a new instance of Decimal to a decimal value represented in binary and contained in a specified array.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Int64) Initializes a new instance of Decimal to the value of the specified 64-bit signed integer.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Single) Initializes a new instance of Decimal to the value of the specified single-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(UInt32) Initializes a new instance of Decimal to the value of the specified 32-bit unsigned integer.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(UInt64) Initializes a new instance of Decimal to the value of the specified 64-bit unsigned integer.
Public method Supported by the XNA Framework Supported by Portable Class Library Decimal(Int32, Int32, Int32, Boolean, Byte) Initializes a new instance of Decimal from parameters specifying the instance's constituent parts.
Top
  Name Description
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Add Adds two specified Decimal values.
Public method Static member Supported by Portable Class Library Ceiling Returns the smallest integral value that is greater than or equal to the specified decimal number.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Compare Compares two specified Decimal values.
Public method Supported by the XNA Framework Supported by Portable Class Library CompareTo(Decimal) Compares this instance to a specified Decimal object.
Public method Supported by the XNA Framework CompareTo(Object) Compares this instance to a specified Object.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Divide Divides two specified Decimal values.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Decimal) Returns a value indicating whether this instance and a specified Decimal object represent the same value.
Public method Supported by the XNA Framework Supported by Portable Class Library Equals(Object) Returns a value indicating whether this instance and a specified Object represent the same type and value. (Overrides ValueType.Equals(Object).)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Equals(Decimal, Decimal) Returns a value indicating whether two specified instances of Decimal represent the same value.
Protected method Supported by the XNA Framework Supported by Portable Class Library Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Floor Rounds a specified Decimal number to the closest integer toward negative infinity.
Public method Static member FromOACurrency Converts the specified 64-bit signed integer, which contains an OLE Automation Currency value, to the equivalent Decimal value.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library GetBits Converts the value of a specified instance of Decimal to its equivalent binary representation.
Public method Supported by the XNA Framework Supported by Portable Class Library GetHashCode Returns the hash code for this instance. (Overrides ValueType.GetHashCode().)
Public method Supported by the XNA Framework Supported by Portable Class Library GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework GetTypeCode Returns the TypeCode for value type Decimal.
Protected method Supported by the XNA Framework Supported by Portable Class Library MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Multiply Multiplies two specified Decimal values.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Negate Returns the result of multiplying the specified Decimal value by negative one.
Public method Static member Supported by the XNA Framework Parse(String) Converts the string representation of a number to its Decimal equivalent.
Public method Static member Supported by the XNA Framework Parse(String, NumberStyles) Converts the string representation of a number in a specified style to its Decimal equivalent.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Parse(String, IFormatProvider) Converts the string representation of a number to its Decimal equivalent using the specified culture-specific format information.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Parse(String, NumberStyles, IFormatProvider) Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Remainder Computes the remainder after dividing two Decimal values.
Public method Static member Round(Decimal) Rounds a decimal value to the nearest integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Round(Decimal, Int32) Rounds a Decimal value to a specified number of decimal places.
Public method Static member Round(Decimal, MidpointRounding) Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two other numbers.
Public method Static member Round(Decimal, Int32, MidpointRounding) Rounds a decimal value to a specified precision. A parameter specifies how to round the value if it is midway between two other numbers.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Subtract Subtracts one specified Decimal value from another.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToByte Converts the value of the specified Decimal to the equivalent 8-bit unsigned integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToDouble Converts the value of the specified Decimal to the equivalent double-precision floating-point number.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToInt16 Converts the value of the specified Decimal to the equivalent 16-bit signed integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToInt32 Converts the value of the specified Decimal to the equivalent 32-bit signed integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToInt64 Converts the value of the specified Decimal to the equivalent 64-bit signed integer.
Public method Static member ToOACurrency Converts the specified Decimal value to the equivalent OLE Automation Currency value, which is contained in a 64-bit signed integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToSByte Converts the value of the specified Decimal to the equivalent 8-bit signed integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToSingle Converts the value of the specified Decimal to the equivalent single-precision floating-point number.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString() Converts the numeric value of this instance to its equivalent string representation. (Overrides ValueType.ToString().)
Public method Supported by the XNA Framework Supported by Portable Class Library ToString(IFormatProvider) Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
Public method Supported by the XNA Framework ToString(String) Converts the numeric value of this instance to its equivalent string representation, using the specified format.
Public method Supported by the XNA Framework Supported by Portable Class Library ToString(String, IFormatProvider) Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToUInt16 Converts the value of the specified Decimal to the equivalent 16-bit unsigned integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToUInt32 Converts the value of the specified Decimal to the equivalent 32-bit unsigned integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library ToUInt64 Converts the value of the specified Decimal to the equivalent 64-bit unsigned integer.
Public method Static member Supported by the XNA Framework Supported by Portable Class Library Truncate Returns the integral digits of the specified Decimal; any fractional digits are discarded.
Public method Static member TryParse(String, Decimal) Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed.
Public method Static member Supported by Portable Class Library TryParse(String, NumberStyles, IFormatProvider, Decimal) Converts the string representation of a number to its Decimal equivalent using the specified style and culture-specific format. A return value indicates whether the conversion succeeded or failed.
Top
  Name Description
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Addition Adds two specified Decimal values.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Decrement Decrements the Decimal operand by one.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Division Divides two specified Decimal values.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Equality Returns a value indicating whether two instances of Decimal are equal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Int16) Converts a Decimal to a 16-bit signed integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Double) Converts a Decimal to a double-precision floating-point number.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to SByte) Converts a Decimal to an 8-bit signed integer.
Public operator Static member Supported by Portable Class Library Explicit(Decimal to Char) Converts a Decimal to a Unicode character.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Int64) Converts a Decimal to a 64-bit signed integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Int32) Converts a Decimal to a 32-bit signed integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to UInt64) Converts a Decimal to a 64-bit unsigned integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to UInt16) Converts a Decimal to a 16-bit unsigned integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Single) Converts a Decimal to a single-precision floating-point number.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to UInt32) Converts a Decimal to a 32-bit unsigned integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Decimal to Byte) Converts a Decimal to an 8-bit unsigned integer.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Double to Decimal) Converts a double-precision floating-point number to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Explicit(Single to Decimal) Converts a single-precision floating-point number to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library GreaterThan Returns a value indicating whether a specified Decimal is greater than another specified Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library GreaterThanOrEqual Returns a value indicating whether a specified Decimal is greater than or equal to another specified Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(Byte to Decimal) Converts an 8-bit unsigned integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(Char to Decimal) Converts a Unicode character to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(Int16 to Decimal) Converts a 16-bit signed integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(Int32 to Decimal) Converts a 32-bit signed integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(Int64 to Decimal) Converts a 64-bit signed integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(SByte to Decimal) Converts an 8-bit signed integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(UInt16 to Decimal) Converts a 16-bit unsigned integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(UInt32 to Decimal) Converts a 32-bit unsigned integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Implicit(UInt64 to Decimal) Converts a 64-bit unsigned integer to a Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Increment Increments the Decimal operand by 1.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Inequality Returns a value indicating whether two instances of Decimal are not equal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library LessThan Returns a value indicating whether a specified Decimal is less than another specified Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library LessThanOrEqual Returns a value indicating whether a specified Decimal is less than or equal to another specified Decimal.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Modulus Returns the remainder resulting from dividing two specified Decimal values.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Multiply Multiplies two specified Decimal values.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Subtraction Subtracts two specified Decimal values.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library UnaryNegation Negates the value of the specified Decimal operand.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library UnaryPlus Returns the value of the Decimal operand (the sign of the operand is unchanged).
Top
  Name Description
Public field Static member Supported by the XNA Framework Supported by Portable Class Library MaxValue Represents the largest possible value of Decimal. This field is constant and read-only.
Public field Static member Supported by the XNA Framework Supported by Portable Class Library MinusOne Represents the number negative one (-1).
Public field Static member Supported by the XNA Framework Supported by Portable Class Library MinValue Represents the smallest possible value of Decimal. This field is constant and read-only.
Public field Static member Supported by the XNA Framework Supported by Portable Class Library One Represents the number one (1).
Public field Static member Supported by the XNA Framework Supported by Portable Class Library Zero Represents the number zero (0).
Top
  Name Description
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToBoolean Infrastructure. For a description of this member, see IConvertible.ToBoolean.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToByte Infrastructure. For a description of this member, see IConvertible.ToByte.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToChar Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDateTime Infrastructure. This conversion is not supported. Attempting to use this method throws an InvalidCastException.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDecimal Infrastructure. For a description of this member, see IConvertible.ToDecimal.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToDouble Infrastructure. For a description of this member, see IConvertible.ToDouble.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt16 Infrastructure. For a description of this member, see IConvertible.ToInt16.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt32 Infrastructure. For a description of this member, see IConvertible.ToInt32.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToInt64 Infrastructure. For a description of this member, see IConvertible.ToInt64.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToSByte Infrastructure. For a description of this member, see IConvertible.ToSByte.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToSingle Infrastructure. For a description of this member, see IConvertible.ToSingle.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToType Infrastructure. For a description of this member, see IConvertible.ToType.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt16 Infrastructure. For a description of this member, see IConvertible.ToUInt16.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt32 Infrastructure. For a description of this member, see IConvertible.ToInt32.
Explicit interface implemetation Private method Supported by the XNA Framework IConvertible.ToUInt64 Infrastructure. For a description of this member, see IConvertible.ToInt64.
Explicit interface implemetation Private method IDeserializationCallback.OnDeserialization Infrastructure. Runs when the deserialization of an object has been completed.
Top

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.


decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor);   


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.


decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 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 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.

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Examples of Currency Value Types With a Decimal Point
using System;
using System.Globalization;
using System.Runtime;
using System.Runtime.Serialization;
using System.Security;


// currency value types:
namespace Currency
{
    [Serializable]
    public struct CAD : IComparable, IFormattable, IConvertible, ISerializable, IComparable<CAD>, IEquatable<CAD>
    {
        /* constructors... */
       /* properties */
        public long WholeNumbers
        {
            get;
        }
        public long DigitsAfterDecimal
        {
            get;
        }
        // other properties and fields...
        /* operators */
        public static CAD operator +(CAD cad1, CAD cad2);
        public static CAD operator +(CAD cad, USD usd);
        public static CAD operator +(CAD cad, decimal m);
        public static CAD operator +(CAD cad, double d);
        public static CAD operator +(CAD cad, float f);
        public static CAD operator +(CAD cad, object o);
        // other operators...
        /* other methods... */
    }
   [Serializable]
    public struct USD : IComparable, IFormattable, IConvertible, ISerializable, IComparable<USD>, IEquatable<USD>
    {
        /* constructors... */
        /* properties */
        public long WholeNumbers
        {
            get;
        }
        public long DigitsAfterDecimal
        {
            get;
        }
        // other properties and fields...
       /* operators... */
       /* other methods... */
    }
    /* other structs of currency value types... */
}
m
It's a little m to make a hard coded decimal in C# code.
decimal x = 100.000003m;
Plus or Minus?
Shouldn't MinValue be -(2^96) + 1?