public struct Decimal : IComparable, IFormattable
Object
ValueType
DecimalThis type implements IFormattable and IComparable.
mscorlib
ExtendedNumerics
Represents a floating-point decimal data type with up to 29 significant digits, suitable for financial and commercial calculations.
The Decimal type can represent values from approximately -7.9 x 1028 to 7.9 x 1028, with 28 or 29 significant digits. The Decimal data type is ideally suited to financial calculations that require a large number of significant digits and no round-off errors.The finite set of values of type decimal are of the form -1s x c x 10-f, where the sign s is 0 or 1, the coefficient c is given by 0 <= c < 296 , and the scale f is such that 0 <= f <= 28.
A Decimal is represented as a 96-bit integer scaled by a power of ten. For a Decimal with an absolute value less than 1.0, the value is exact to the 28th decimal place, but no further. For a Decimal with an absolute value greater than or equal to 1.0, the value is exact to 28 or 29 digits.
The result of an operation on values of type Decimal is that which would result from calculating an exact result (preserving scale, as defined for each operator) and then rounding to fit the representation. That is, results are exact to 28 or 29 digits, but to no more than 28 decimal places. A zero result has a sign of 0 and a scale of 0.
Results are rounded to the nearest representable value, and, when a result is equally close to two representable values, to the value that has an even number in the least significant digit position (banker's rounding).
[Note: Unlike the Single and Double data types, decimal fractional numbers such as 0.1 can be represented exactly in the Decimal representation. In the Single and Double representations, such numbers are often infinite fractions, making those representations prone to round-off errors.
Further, the Decimal representation preserves scale, so that 1.23 + 1.27 will give the answer 2.50, not 2.5.
]
If a Decimal arithmetic operation produces a value that is too small for the Decimal format after rounding, the result of the operation is zero. If a Decimal arithmetic operation produces a result that is too large for the Decimal format, a OverflowException is thrown.
[Note: The Decimal class implements implicit conversions from the SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, and UInt64 types to Decimal. These implicit conversions never lose information and never throw exceptions. The Decimal class also implements explicit conversions from Decimal to Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, and UInt64. These explicit conversions round the Decimal value towards zero to the nearest integer, and then convert that integer to the destination type. A OverflowException is thrown if the result is not within the range of the destination type.
The Decimal class provides narrowing conversions to and from the Single and Double types. A conversion from Decimal to Single or Double may lose precision, but will not lose information about the overall magnitude of the numeric value, and will never throw an exception. A conversion from Single or Double to Decimal throws a OverflowException if the value is not within the range of the Decimal type.
]
System Namespace
Decimal Constructors
Decimal(int) Constructor
Decimal(uint) Constructor
Decimal(long) Constructor
Decimal(ulong) Constructor
Decimal(float) Constructor
Decimal(double) Constructor
Decimal(int[]) Constructor
Decimal Methods
Decimal.Add Method
Decimal.Compare Method
Decimal.CompareTo Method
Decimal.Divide Method
Decimal.Equals(System.Object) Method
Decimal.Equals(System.Decimal, System.Decimal) Method
Decimal.Floor Method
Decimal.GetBits Method
Decimal.GetHashCode Method
Decimal.Multiply Method
Decimal.Negate Method
Decimal.Parse(System.String) Method
Decimal.Parse(System.String, System.Globalization.NumberStyles) Method
Decimal.Parse(System.String, System.IFormatProvider) Method
Decimal.Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider) Method
Decimal.Remainder Method
Decimal.Round Method
Decimal.Subtract Method
Decimal.ToString(System.IFormatProvider) Method
Decimal.ToString(System.String, System.IFormatProvider) Method
Decimal.ToString() Method
Decimal.ToString(System.String) Method
Decimal.Truncate Method
Decimal.op_Addition Method
Decimal.op_Decrement Method
Decimal.op_Division Method
Decimal.op_Equality Method
System.Decimal Decimal.op_Explicit(float) Method
System.Decimal Decimal.op_Explicit(double) Method
byte Decimal.op_Explicit(System.Decimal) Method
sbyte Decimal.op_Explicit(System.Decimal) Method
char Decimal.op_Explicit(System.Decimal) Method
short Decimal.op_Explicit(System.Decimal) Method
ushort Decimal.op_Explicit(System.Decimal) Method
int Decimal.op_Explicit(System.Decimal) Method
uint Decimal.op_Explicit(System.Decimal) Method
long Decimal.op_Explicit(System.Decimal) Method
ulong Decimal.op_Explicit(System.Decimal) Method
float Decimal.op_Explicit(System.Decimal) Method
double Decimal.op_Explicit(System.Decimal) Method
Decimal.op_GreaterThan Method
Decimal.op_GreaterThanOrEqual Method
System.Decimal Decimal.op_Implicit(byte) Method
System.Decimal Decimal.op_Implicit(sbyte) Method
System.Decimal Decimal.op_Implicit(short) Method
System.Decimal Decimal.op_Implicit(ushort) Method
System.Decimal Decimal.op_Implicit(char) Method
System.Decimal Decimal.op_Implicit(int) Method
System.Decimal Decimal.op_Implicit(uint) Method
System.Decimal Decimal.op_Implicit(long) Method
System.Decimal Decimal.op_Implicit(ulong) Method
Decimal.op_Increment Method
Decimal.op_Inequality Method
Decimal.op_LessThan Method
Decimal.op_LessThanOrEqual Method
Decimal.op_Modulus Method
Decimal.op_Multiply Method
Decimal.op_Subtraction Method
Decimal.op_UnaryNegation Method
Decimal.op_UnaryPlus Method
Decimal Fields
Decimal.MaxValue Field
Decimal.MinValue Field
Decimal.MinusOne Field
Decimal.One Field
Decimal.Zero Field
public Decimal(int value);
Constructs and initializes a new Decimal value.
- value
- The Int32 value used to initialize the new Decimal.
This constructor initializes the new Decimal to the value specified by value.
System.Decimal Structure, System Namespace
public Decimal(uint value);
Constructs and initializes a new Decimal value.
- value
- The UInt32 value used to initialize the new Decimal.
This member is not CLS-compliant. For a CLS-compliant alternative, use the Decimal(Int64) constructor.This constructor initializes the new Decimal to the value specified by value.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public Decimal(long value);
Constructs and initializes a new Decimal value.
- value
- The Int64 value used to initialize the new Decimal.
This constructor initializes the new Decimal to the value specified by value.
System.Decimal Structure, System Namespace
public Decimal(ulong value);
Constructs and initializes a new Decimal value.
- value
- The UInt64 value used to initialize the new Decimal.
This constructor initializes the new Decimal to the value specified by value.This member is not CLS-compliant. For a CLS-compliant alternative, use the Decimal(Int64) constructor.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public Decimal(float value);
Constructs and initializes a new Decimal value.
- value
- The Single value used to initialize the new Decimal.
Exception Type Condition OverflowException value is one of the following: greater than System.Decimal.MaxValue
less than System.Decimal.MinValue
equal to System.Single.NaN
equal to System.Single.PositiveInfinity
equal to System.Single.NegativeInfinity
This constructor initializes the new Decimal to the value specified by value. This constructor rounds value to 7 significant digits using banker's rounding.
System.Decimal Structure, System Namespace
public Decimal(double value);
Constructs and initializes a new Decimal value.
- value
- The Double value used to initialize the new Decimal.
Exception Type Condition OverflowException value is one of the following: greater than System.Decimal.MaxValue
less than System.Decimal.MinValue
equal to System.Double.NaN
equal to System.Double.PositiveInfinity
equal to System.Double.NegativeInfinity
This constructor initializes the new Decimal to the value specified by value. This constructor rounds value to 15 significant digits using banker's rounding.
System.Decimal Structure, System Namespace
public Decimal(int[] bits);
Constructs and initializes a new Decimal value.
- bits
- A Int32 array containing a bit representation of a Decimal. bits contains the following four elements:
Index 0 (bits 0-31) contains the low-order 32 bits of the decimal's coefficient.
Index 1 (bits 32-63) contains the middle 32 bits of the decimal's coefficient.
Index 2 (bits 64-95) contains the high-order 32 bits of the decimal's coefficient.
Index 3 (bits 96-127) contains the sign bit and scale. See below.
Exception Type Condition ArgumentNullException bits is a null reference. ArgumentException bits does not contain four elements.
This constructor initializes the new Decimal to the value represented by the elements of bits .[Note: The elements of bits represent a Decimal as a coefficient c , a scale f, and sign bit s. The decimal value is computed as -1s x c x 10-f. The coefficient c is divided into three 32-bit integer values.]
The sign and scale occupy a total of 32 bits as follows:
[Note: A numeric value may have several possible binary representations; they are numerically equal but have different scales. Also, the bit representation differentiates between -0, 0.00, and 0; these are all treated as 0 in operations, and any zero result will have a sign of 0 and a scale of 0.]
Bit Positions Name 0-15 (None.) 16-23 Scale 24-30 (None.) 31 Sign
The following example demonstrates using the Decimal (Int32 []) constructor.
using System; class ConstructDecimal { public static void Main() { int negativeBitValue = unchecked ((int)0x80000000); int [] parts0 = {0,0,0,0}; //Positive Zero. int [] parts1 = {1,0,0,0}; int [] parts2 = {0,1,0,0}; int [] parts3 = {0,0,1,0}; int [] parts4 = {0,0,0,negativeBitValue}; // Negative zero. int [] parts5 = {1,1,1,0}; int [] partsMaxValue = {-1,-1,-1,0}; int [] partsMinValue = {-1,-1,-1,negativeBitValue}; decimal d = new Decimal(parts0); Console.WriteLine("{{0,0,0,0}} = {0}",d); d = new Decimal(parts1); Console.WriteLine("{{1,0,0,0}} = {0}",d); d = new Decimal(parts2); Console.WriteLine("{{0,1,0,0}} = {0}",d); d = new Decimal(parts3); Console.WriteLine("{{0,0,1,0}} = {0}",d); d = new Decimal(parts4); Console.WriteLine("{{0,0,0,{0}}} = {1}",parts4[3],d); d = new Decimal(parts5); Console.WriteLine("{{1,1,1,0}} = {0}",d); d = new Decimal(partsMaxValue ); Console.WriteLine("{{-1,-1,-1,0}} = {0}",d); d = new Decimal(partsMinValue); Console.WriteLine("{{-1,-1,-1,{0}}} = {1}",partsMinValue [3],d); } }The output is
{0,0,0,0} = 0
{1,0,0,0} = 1
{0,1,0,0} = 4294967296
{0,0,1,0} = 18446744073709551616
{0,0,0,-2147483648} = 0
{1,1,1,0} = 18446744078004518913
{-1,-1,-1,0} = 79228162514264337593543950335
{-1,-1,-1,-2147483648} = -79228162514264337593543950335
System.Decimal Structure, System Namespace
public static decimal Add(decimal d1, decimal d2);
Adds two Decimal values together.
- d1
- The first value to add..
- d2
- The second value to add.
A Decimal containing the sum of d1 and d2. The scale of the result, before any rounding, is the larger of the scales of d1 and d2. For example, 1.1 + 2.22 gives 3.32, and 2.50 + 1 gives 3.50.
Exception Type Condition OverflowException The sum of d1 and d2 is less than System.Decimal.MinValue or greater than System.Decimal.MaxValue.
System.Decimal Structure, System Namespace
public static int Compare(decimal d1, decimal d2);
Compares the values of two Decimal values and returns sort order information.
- d1
- The first value to compare.
- d2
- The second value to compare.
A Int32 containing a value that reflects the sort order of d1 and d2. The exact value returned by this method is implementation defined. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number. Each comparison compares the numerical values of d1 and d2.
Return Value Meaning Any negative number d1< d2 Zero d1 == d2 Any positive number d1>d2
System.Decimal Structure, System Namespace
public int CompareTo(object value);
Returns the sort order of the current instance compared to the specified Object .
- value
- The Object to compare to the current instance.
A Int32 containing a value that reflects the sort order of the current instance as compared to value. The following table defines the conditions under which the returned value is a negative number, zero, or a positive number. Each comparison compares the numerical values of d1 and d2.
Return Value Description Any negative number Current instance < value. Zero Current instance == value. Any positive number current instance > value, or value is a null reference.
Exception Type Condition ArgumentException value is not a Decimal and is not a null reference.
[Note: This method is implemented to support the IComparable interface.]
System.Decimal Structure, System Namespace
public static decimal Divide(decimal d1, decimal d2);
Divides the value of one Decimal by another.
- d1
- The dividend.
- d2
- The divisor.
A Decimal containing the result of dividing d1 by d2 . The scale of the result, before any rounding, is the smallest scale that will preserve a result equal to the exact result. For example, 2.22 / 2 gives 1.11.
Exception Type Condition DivideByZeroException d2 is zero. OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public override bool Equals(object value);
Determines whether the current instance and the specified Object have the same type and value.
- value
- The Object to compare to the current instance.
true
if value has the same type and is numerically equal to (has the same value as) the current instance. If value is a null reference or is not an instance of Decimal, returnsfalse
.
[Note: This method overrides System.Object.Equals(System.Object).]
System.Decimal Structure, System Namespace
public static bool Equals(decimal d1, decimal d2);
Determines whether two Decimal values have the same value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if d1 and d2 are numerically equal (have the same value), otherwisefalse
.
System.Decimal Structure, System Namespace
public static decimal Floor(decimal d);
Rounds a Decimal value to the closest integer towards negative infinity.
- d
- The Decimal to round downward.
A Decimal value v such that v is a whole number and d - 1 < v <= d . The scale of the result will be zero.
The following example demonstrates the System.Decimal.Floor(System.Decimal) method.
using System; class DecimalTest { public static void Main() { Console.WriteLine("floor {0} is {1}", 3.14159m, Decimal.Floor(3.14159m)); Console.WriteLine("floor {0} is {1}", -3.9m, Decimal.Floor(-3.9m)); Console.WriteLine("floor {0} is {1}", 3.0m, Decimal.Floor(3.0m)); } }The output is
floor 3.14159 is 3
floor -3.9 is -4
floor 3.0 is 3
System.Decimal Structure, System Namespace
public static int[] GetBits(decimal d);
Returns a binary representation of the specified Decimal value.
- d
- The Decimal value for which a binary representation is returned.
An array of type Int32 containing the following four elements:
The sign bit and scale occupy 32 bits as follows:
Index Description 0 Low-order 32 bits of the decimal's coefficient, c. 1 Middle 32 bits of c. 2 High-order 32 bits of c. 3 Sign bit and scale. See below.
Bit Positions Description 0-15 Unused. 16-23 Contains the scale f, a value between 0 and 28 which indicates the power of 10 to divide c by, to produce the Decimal value. 24-30 Unused. 31 The sign, s , of the Decimal value. 0 (zero or positive) or 1 (negative).
[Note: The elements of the returned array contain a binary representation of d as a coefficient c a scale f and sign bit s. The value of d is computed as -1s x c x 10-f .c occupies the first three elements of the returned array. The fourth element contains f and s .
]
A numeric value may have several possible binary representations; they are numerically equal but have different scales. Also, the bit representation differentiates between -0, 0.00, and 0; these are all treated as 0 in operations, and any zero result will have a sign of 0 and a scale of 0.
The following example demonstrates the different representations of 1.00 and 1.
using System; public class Class1 { public static void Print (int [] bs) { foreach (int b in bs) { Console.Write (b+" "); } } public static void Main () { decimal d = 1.00m; decimal d1 = 1; Console.Write (d); Console.Write (" - bits: "); Print (decimal.GetBits(d)); Console.WriteLine(); Console.Write (d1); Console.Write (" - bits: "); Print (decimal.GetBits(d1)); Console.WriteLine(); Console.WriteLine ("d1.CompareTo(d) == {0}", d1.CompareTo(d)); Console.WriteLine ("d1 == d {0}", d1 == d); } }The output is
1.00 - bits: 100 0 0 512
1 - bits: 1 0 0 0
d1.CompareTo(d) == 0
d1 == d True
System.Decimal Structure, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for this instance.
The algorithm used to generate the hash code value is unspecified.[Note: This method overrides System.Object.GetHashCode.]
System.Decimal Structure, System Namespace
public static decimal Multiply(decimal d1, decimal d2);
Returns the result of multiplying two Decimal values.
- d1
- The first value to multiply.
- d2
- The second value to multiply.
The result of multiplying d1 and d2 . The scale of the result, before any rounding, is the sum of the scales of d1 and d2.For example, 123 x 3 gives 369, and 2.2 x 1.35 gives 2.970.
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static decimal Negate(decimal d);
Returns the result of multiplying a Decimal value by negative one.
- d
- The value to negate.
The negated value of d. If d is zero then zero is returned (with 0 sign and scale); otherwise the scale of the result is the same as the scale of d .
System.Decimal Structure, System Namespace
public static decimal Parse(string s);
Returns the specified String converted to a Decimal value.
- s
- A String containing the value to convert. The string is interpreted using the System.Globalization.NumberStyles.Number style, preserving scale.
The Decimal value obtained from s.
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct format. OverflowException s represents a number greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
This version of System.Decimal.Parse(System.String) is equivalent to System.Decimal.Parse(System.String) (s , System.Globalization.NumberStyles.Number ,null
).The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo .]
If necessary, the value of s is rounded using banker's rounding. Any scale apparent in the string s is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence the string "2.900" will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
The following example demonstrates the System.Decimal.Parse(System.String) method.
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s1 = " -1.001 "; string s2 = "+1,000,111.99"; string s3 = "2.900"; Console.WriteLine("String: {0} (decimal) {1}",s1,Decimal.Parse(s1)); Console.WriteLine("String: {0} (decimal) {1}",s2,Decimal.Parse(s2)); Console.WriteLine("String: {0} (decimal) {1}",s3,Decimal.Parse(s3)); } }The output is
String: -1.001 (decimal) -1.001
String: +1,000,111.99 (decimal) 1000111.99
String: 2.900 (decimal) 2.900
System.Decimal Structure, System Namespace
public static decimal Parse(string s, NumberStyles style);
Returns the specified String converted to a Decimal value.
- s
- A String containing the value to convert. The string is interpreted using the style specified by style , preserving scale.
- style
- Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Number style.
The Decimal value obtained from s .
Exception Type Condition ArgumentNullException s is a null reference. FormatException s is not in the correct style. OverflowException s represents a number greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
This version of System.Decimal.Parse(System.String) is equivalent to System.Decimal.Parse(System.String) (s, style ,null
).The string s is parsed using the formatting information in a NumberFormatInfo initialized for the current system culture. [Note: For more information, see System.Globalization.NumberFormatInfo.CurrentInfo .]
If necessary, the value of s is rounded using banker's rounding.
The following example demonstrates supplying NumberStyles values to the System.Decimal.Parse(System.String) method to allow for a symbol separating groups of digits, and a decimal separator. This example uses the symbols from the U.S. English culture, namely a comma and a decimal point.
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s = "1,000,111.99"; NumberStyles ns = NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint; decimal d = Decimal.Parse(s,ns); Console.WriteLine("{0} parsed to decimal {1}",s,d); } }The output is
1,000,111.99 parsed to decimal 1000111.99
System.Decimal Structure, System Namespace
public static decimal Parse(string s, IFormatProvider provider);
Returns the specified String converted to a Decimal value.
- s
- A String containing the value to convert. The String is interpreted using the System.Globalization.NumberStyles.Number style, preserving scale.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.
The Decimal value obtained from s .
Exception Type Condition FormatException s is not in the correct style. OverflowException s represents a number greater than System.Decimal.MaxValue or less than System.Decimal.MinValue. ArgumentNullException s is a null reference.
This version of System.Decimal.Parse(System.String) is equivalent to System.Decimal.Parse(System.String) (s, System.Globalization.NumberStyles.Number , provider ).The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider is
null
or a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.If necessary, the value of s is rounded using banker's rounding. Any scale apparent in the string s is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence the string "2.900" will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
The following example demonstrates supplying a IFormatProvider to the System.Decimal.Parse(System.String) method to allow a decimal point, and commas separating groups of digits.
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s = "1,000,111.99"; //Get the default formatting symbols. NumberFormatInfo nfi = new NumberFormatInfo(); // Default group separator is ',' // Default decimal separator is '.' decimal d = Decimal.Parse(s,nfi); Console.WriteLine("{0} parsed to decimal {1}",s,d); } }The output is
1,000,111.99 parsed to decimal 1000111.99
System.Decimal Structure, System Namespace
public static decimal Parse(string s, NumberStyles style, IFormatProvider provider);
Returns the specified String converted to a Decimal value.
- s
- A String containing the value to convert. The string is interpreted using the style specified by style , preserving scale.
- style
- Zero or more NumberStyles values that specify the style of s. Specify multiple values for style using the bitwise OR operator. If style is a null reference, the string is interpreted using the System.Globalization.NumberStyles.Number style.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information about s.
The Decimal value obtained from s .
Exception Type Condition ArgumentNullException s is a null reference.
FormatException s is not in the correct style. OverflowException s represents a number greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
The string s is parsed using the culture-specific formatting information from the NumberFormatInfo instance supplied by provider. If provider isnull
or if a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.If necessary, the value of s is rounded using banker's rounding.
The following example demonstrates supplying NumberStyles values and a IFormatProvider to the System.Decimal.Parse(System.String) method to allow colons separating groups of digits, and a decimal point.
using System; using System.Globalization; class DecimalParseClass { public static void Main() { string s = "1:000:111.99"; NumberStyles ns = NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint; NumberFormatInfo nfi = new NumberFormatInfo(); //Change the format info to separate digit groups using a colon. nfi.NumberGroupSeparator = ":"; decimal d = Decimal.Parse(s,ns,nfi); Console.WriteLine("{0} parsed to decimal {1}",s,d); } }The output is
1:000:111.99 parsed to decimal 1000111.99
System.Decimal Structure, System Namespace
public static decimal Remainder(decimal d1, decimal d2);
Computes the remainder after dividing two Decimal values.
- d1
- The dividend.
- d2
- The divisor.
The remainder after dividing d1 by d2 to give an integer result. The sign of the result, if non-zero, is the same as the sign of d1, and the scale of the result is the same as the scale of d2.For example, -10 % 3 gives -1, and 3.6 % 1.3 gives 1.0 (where % indicates the remainder operation).
Exception Type Condition DivideByZeroException d2 is zero. OverflowException d1 divided by d2 is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static decimal Round(decimal d, int decimals);
Rounds a Decimal value to a specified number of decimal places.
- d
- The Decimal to round.
- decimals
- The number of decimal places to round to. 0 <= decimals <= 28.
The Decimal result of rounding d to decimals decimal places.
Exception Type Condition ArgumentOutOfRangeException decimals is not between 0 and 28, inclusive.
When d is exactly half way between two rounded values, the result is the rounded value that has an even digit in the rightmost decimal position. For example, when rounded to two decimals, the value 2.345 becomes 2.34 and the value 2.355 becomes 2.36. [Note: This process is known as rounding half towards even, or banker's rounding.]
The scale of the result will be the smaller of decimals and the scale of d.
[Note: The scale of d is never increased, so System.Decimal.Round(System.Decimal,System.Int32) cannot cause overflow.]
The following example demonstrates the System.Decimal.Round(System.Decimal,System.Int32) method.
using System; class MyClass { public static void Main() { decimal d1 = 2.5m; decimal d2 = 3.5m; decimal d3 = 2.98765432m; decimal d4 = 2.18765432m; Console.WriteLine("Rounding to 0 places..."); Console.WriteLine("round {0} = {1}",d1, Decimal.Round(d1,0)); Console.WriteLine("round {0} = {1}",d2, Decimal.Round(d2,0)); Console.WriteLine("round {0} = {1}",d3, Decimal.Round(d3,0)); Console.WriteLine("round {0} = {1}",d4, Decimal.Round(d4,0)); Console.WriteLine("Rounding to 2 places..."); Console.WriteLine("round {0} = {1}",d1, Decimal.Round(d1,2)); Console.WriteLine("round {0} = {1}",d2, Decimal.Round(d2,2)); Console.WriteLine("round {0} = {1}",d3, Decimal.Round(d3,2)); Console.WriteLine("round {0} = {1}",d4, Decimal.Round(d4,2)); } }The output is
Rounding to 0 places...
round 2.5 = 2
round 3.5 = 4
round 2.98765432 = 3
round 2.18765432 = 2
Rounding to 2 places...
round 2.5 = 2.5
round 3.5 = 3.5
round 2.98765432 = 2.99
round 2.18765432 = 2.19
System.Decimal Structure, System Namespace
public static decimal Subtract(decimal d1, decimal d2);
Subtracts one Decimal value from another.
- d1
- The left-side operand.
- d2
- The right-side operand.
A Decimal containing the result of subtracting d2 from d1 . The scale of the result, before any rounding, is the larger of the scales of d1 and d2.For example, 1.1 - 2.22 gives -1.12, and 2.50 - 1 gives 1.50.
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public string ToString(IFormatProvider provider);
Returns a String representation of the value of the current instance.
- provider
- A IFormatProvider that supplies a NumberFormatInfo containing culture-specific formatting information.
A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the formatting information in the NumberFormatInfo instance supplied by provider.
This version of System.Decimal.ToString is equivalent to System.Decimal.ToString (null
, provider ).If provider is
null
or if a NumberFormatInfo cannot be obtained from provider, the formatting information for the current system culture is used.[Note: The general format specifier formats the number in either fixed-point or exponential notation form. For a detailed description of the general format, see the IFormattable interface.]
System.Decimal Structure, System Namespace
public string ToString(string format, IFormatProvider provider);
Returns a String representation of the value of the current instance.
- format
- A String containing a character that specifies the format of the returned string, optionally followed by a non-negative integer that specifies the precision of the number in the returned String .
- provider
- A IFormatProvider that supplies a NumberFormatInfo instance containing culture-specific formatting information.
A String representation of the current instance formatted as specified by format. The string takes into account the information in the NumberFormatInfo instance supplied by provider.
Exception Type Condition FormatException format is invalid.
If provider isnull
or if a NumberFormatInfo cannot be obtained from provider , the formatting information for the current system culture is used.The following table lists the characters that are valid for the format parameter.
If format is a null reference, the general format specifier "G" is used.
Format Characters Description "C", "c" Currency format. "Z", "z" Normalize format (trims trailing zeros). "E", "e" Exponential notation format. "F", "f" Fixed-point format. "G", "g" General format. "N", "n" Number format. "P", "p" Percent format. [Note: For a detailed description of formatting, see the IFormattable interface.
This method is implemented to support the IFormattable interface.
]
System.Decimal Structure, System Namespace
public override string ToString();
Returns a canonical String representation of the value of the current instance.
A String representation of the current instance formatted using the general format specifier, ("G"). The string takes into account the current system culture and preserves the scale of the number.
This version of System.Decimal.ToString is equivalent to System.Decimal.ToString (null
,null
).[Note: The general format specifier formats the number in either fixed-point or exponential notation form, preserving the scale of the number. For a detailed description of the general format, see the IFormattable interface.
This method overrides System.Object.ToString .
]
System.Decimal Structure, System Namespace
public string ToString(string format);
Returns a String representation of the value of the current instance.
- format
- A String that specifies the format of the returned string. [Note: For a list of valid values, see System.Decimal.ToString (String, IFormatProvider ).]
A String representation of the current instance formatted as specified by format. The string takes into account the current system culture.
Exception Type Condition FormatException format is invalid.
This version of System.Decimal.ToString is equivalent to System.Decimal.ToString (format ,null
).If format is a null reference, the general format specifier "G" is used.
The following example shows the effects of various formats on the string returned by System.Decimal.ToString.
using System; class test { public static void Main() { decimal d = 1234.56789m; Console.WriteLine(d); string[] fmts = {"C","E","F","G","N","P"}; for (int i=0;i<fmts.Length;i++) Console.WriteLine("{0}: {1}", fmts[i],d.ToString(fmts[i])); } }The output is
1234.56789
C: $1,234.57
E: 1.234568E+003
F: 1234.57
G: 1234.56789
N: 1,234.57
P: 123,456.79 %
System.Decimal Structure, System Namespace
public static decimal Truncate(decimal d);
Rounds a Decimal value towards zero, to the closest integer value.
- d
- The Decimal to truncate.
The Decimal result of truncating d . the scale of the result is 0.
The following example demonstrates using the System.Decimal.Truncate(System.Decimal) method.
using System; class MyClass { public static void Main() { decimal d1 = 1234.56789m; decimal d2 = -1234.56789m; Console.WriteLine("{0} truncated is {1}", d1, Decimal.Truncate(d1)); Console.WriteLine("{0} truncated is {1}", d2, Decimal.Truncate(d2)); } }The output is
1234.56789 truncated is 1234
-1234.56789 truncated is -1234
System.Decimal Structure, System Namespace
public static Decimal operator +(Decimal d1, Decimal d2);
Adds two Decimal values together.
- d1
- The first value to add.
- d2
- The second value to add.
The value returned by System.Decimal.Add(System.Decimal,System.Decimal) (d1,d2).
Exception Type Condition OverflowException The sum of d1 and d2 is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static Decimal operator --(Decimal d);
Returns the specified value decremented by one.
- d
- A Decimal value.
The value returned by System.Decimal.Subtract(System.Decimal,System.Decimal) (d , System.Decimal.One ).
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue .
System.Decimal Structure, System Namespace
public static Decimal operator /(Decimal d1, Decimal d2);
Divides one Decimal value by another Decimal .
- d1
- The dividend.
- d2
- The divisor.
The value returned by System.Decimal.Divide(System.Decimal,System.Decimal) (d1, d2).
Exception Type Condition DivideByZeroException The divisor is zero. OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static bool operator ==(Decimal d1, Decimal d2);
Determines whether two decimals have the same value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2 ) returns zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static explicit operator Decimal(float value);
Perform an explicit conversion of a Single value to Decimal.
- value
- The Single value to convert to Decimal .
A Decimal with the specified value.
Exception Type Condition OverflowException value is one of the following: greater than System.Decimal.MaxValue
less than System.Decimal.MinValue
equal to System.Single.NaN
equal to System.Single.PositiveInfinity
equal to System.Single.NegativeInfinity
System.Decimal Structure, System Namespace
public static explicit operator Decimal(double value);
Perform an explicit conversion of a Double value to Decimal.
- value
- The Double value to convert to Decimal .
A Decimal with the specified value.
Exception Type Condition OverflowException value is one of the following: greater than System.Decimal.MaxValue
less than System.Decimal.MinValue
equal to System.Double.NaN
equal to System.Double.PositiveInfinity
equal to System.Double.NegativeInfinity
System.Decimal Structure, System Namespace
public static explicit operator byte(Decimal value);
Perform an explicit conversion of a Decimal value to Byte .
- value
- The Decimal value to convert to Byte .
A Byte containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.Byte.MaxValue or less than System.Byte.MinValue.
System.Decimal Structure, System Namespace
public static explicit operator sbyte(Decimal value);
Perform an explicit conversion of a Decimal value to SByte .
- value
- The Decimal value to convert to SByte.
A SByte containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.SByte.MaxValue or less than System.SByte.MinValue.
This member is not CLS-compliant. For a CLS-compliant alternative to SByte, use Int16 .
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static explicit operator char(Decimal value);
Perform an explicit conversion of a Decimal value to Char .
- value
- The Decimal value to convert to Char .
A Char containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.Char.MaxValue or less than System.Char.MinValue.
System.Decimal Structure, System Namespace
public static explicit operator short(Decimal value);
Perform an explicit conversion of a Decimal value to Int16 .
- value
- The Decimal value to convert to Int16 .
A Int16 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.Int16.MaxValue or less than System.Int16.MinValue.
System.Decimal Structure, System Namespace
public static explicit operator ushort(Decimal value);
Perform an explicit conversion of a Decimal value to UInt16 .
- value
- The Decimal value to convert to UInt16 .
A UInt16 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.UInt16.MaxValue or less than System.UInt16.MinValue.
This member is not CLS-compliant. For a CLS-compliant alternative to UInt16, use Int32 .
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static explicit operator int(Decimal value);
Perform an explicit conversion of a Decimal value to Int32 .
- value
- The Decimal value to convert to Int32 .
A Int32 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.Int32.MaxValue or less than System.Int32.MinValue.
System.Decimal Structure, System Namespace
public static explicit operator uint(Decimal value);
Perform an explicit conversion of a Decimal value to UInt32 .
- value
- The Decimal value to convert to UInt32 .
A UInt32 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.UInt32.MaxValue or less than System.UInt32.MinValue.
This member is not CLS-compliant. For a CLS-compliant alternative to UInt32, use Int64 ).
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static explicit operator long(Decimal value);
Perform an explicit conversion of a Decimal value to Int64 .
- value
- The Decimal value to convert to Int64 .
A Int64 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.Int64.MaxValue or less than System.Int64.MinValue.
System.Decimal Structure, System Namespace
public static explicit operator ulong(Decimal value);
Perform an explicit conversion of a Decimal value to UInt64 .
- value
- The Decimal value to convert to UInt64 .
A UInt64 containing value rounded towards zero to the nearest integer.
Exception Type Condition OverflowException The resulting integer value is greater than System.UInt64.MaxValue or less than System.UInt64.MinValue.
This member is not CLS-compliant. For a CLS-compliant alternative to UInt64, use Int64 .
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static explicit operator float(Decimal value);
Perform an explicit conversion of a Decimal value to Single .
- value
- The Decimal value to convert to Single .
A Single with the specified value.[Note: This operation can produce round-off errors due to the fact that Single has fewer significant digits than Decimal.]
System.Decimal Structure, System Namespace
public static explicit operator double(Decimal value);
Perform an explicit conversion of a Decimal value to Double .
- value
- The Decimal value to convert to Double .
A Double with the specified value.
[Note: This operation can produce round-off errors due to the fact that Double has fewer significant digits than Decimal.]
System.Decimal Structure, System Namespace
public static bool operator >(Decimal d1, Decimal d2);
Determines whether one Decimal value is greater than another Decimal value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2) returns a value that is greater than zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static bool operator >=(Decimal d1, Decimal d2);
Determines whether one Decimal value is greater than or equal to another Decimal value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2) returns a value that is greater than or equal to zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(byte value);
Perform an implicit conversion of a Byte value to Decimal.
- value
- The Byte value to convert to Decimal .
A Decimal with the specified value.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(sbyte value);
Perform an implicit conversion of a SByte value to Decimal.
- value
- The SByte value to convert to Decimal .
A Decimal with the specified value.
This member is not CLS-compliant.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static implicit operator Decimal(short value);
Perform an implicit conversion of a Int16 value to Decimal.
- value
- The Int16 value to convert to Decimal.
A Decimal with the specified value.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(ushort value);
Perform an implicit conversion of a UInt16 value to Decimal.
- value
- The UInt16 value to convert to Decimal .
A Decimal with the specified value.
This member is not CLS-compliant.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static implicit operator Decimal(char value);
Perform an implicit conversion of a Char value to Decimal.
- value
- The Char value to convert to Decimal .
A Decimal with the specified value.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(int value);
Perform an implicit conversion of a Int32 value to Decimal.
- value
- The Int32 value to convert to Decimal .
A Decimal with the specified value.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(uint value);
Perform an implicit conversion of a UInt32 value to Decimal.
- value
- The UInt32 value to convert to Decimal .
A Decimal with the specified value.
This member is not CLS-compliant.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static implicit operator Decimal(long value);
Perform an implicit conversion of a Int64 value to Decimal.
- value
- The Int64 value to convert to Decimal .
A Decimal with the specified value.
System.Decimal Structure, System Namespace
public static implicit operator Decimal(ulong value);
Perform an implicit conversion of a UInt64 value to Decimal.
- value
- The UInt64 value to convert to Decimal .
A Decimal with the specified value.
This member is not CLS-compliant.
CLSCompliantAttribute(false)
System.Decimal Structure, System Namespace
public static Decimal operator ++(Decimal d);
Returns the specified value incremented by one.
- d
- A Decimal value.
The value returned by System.Decimal.Add(System.Decimal,System.Decimal) (d , System.Decimal.One ).
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue .
System.Decimal Structure, System Namespace
public static bool operator !=(Decimal d1, Decimal d2);
Determines whether two decimals do not have the same value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2) does not return zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static bool operator <(Decimal d1, Decimal d2);
Determines whether one Decimal value is less than another Decimal value.
- d1
- The first Decimal to compare.
- d2
- The first Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2) returns a value that is less than zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static bool operator <=(Decimal d1, Decimal d2);
Determines whether one Decimal value is less than or equal to another Decimal value.
- d1
- The first Decimal to compare.
- d2
- The second Decimal to compare.
true
if System.Decimal.Compare(System.Decimal,System.Decimal) (d1, d2) returns a value that is less than or equal to zero; otherwisefalse
.
System.Decimal Structure, System Namespace
public static Decimal operator %(Decimal d1, Decimal d2);
Divides one Decimal value by another Decimal and returns the remainder.
- d1
- The dividend.
- d2
- The divisor.
The value returned by System.Decimal.Remainder(System.Decimal,System.Decimal) (d1, d2).
Exception Type Condition DivideByZeroException d2 is zero. OverflowException d1 divided by d2 is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static Decimal operator *(Decimal d1, Decimal d2);
Returns the result of multiplying two Decimal values.
- d1
- The first operand.
- d2
- The second operand.
The value returned by System.Decimal.Multiply(System.Decimal,System.Decimal) (d1, d2).
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static Decimal operator -(Decimal d1, Decimal d2);
Subtracts one Decimal value from another.
- d1
- The left-side operand.
- d2
- The right-side operand.
The value returned by System.Decimal.Subtract(System.Decimal,System.Decimal) (d1, d2 ).
Exception Type Condition OverflowException The result is greater than System.Decimal.MaxValue or less than System.Decimal.MinValue.
System.Decimal Structure, System Namespace
public static Decimal operator -(Decimal d);
Returns the specified value multiplied by negative one (-1).
- d
- A Decimal value.
The value returned by System.Decimal.Negate(System.Decimal) (d).
System.Decimal Structure, System Namespace
public static Decimal operator +(Decimal d);
Returns the specified value.
- d
- A Decimal value.
Returns d.
System.Decimal Structure, System Namespace
public static readonly decimal MaxValue;
Contains the maximum positive value for the Decimal type.
The value of this constant is 79228162514264337593543950335.This field is read-only.
DecimalConstantAttribute(79228162514264337593543950335, 79228162514264337593543950335, 79228162514264337593543950335, 79228162514264337593543950335, 79228162514264337593543950335)
System.Decimal Structure, System Namespace
public static readonly decimal MinValue;
Contains the minimum (most negative) value for the Decimal type.
The value of this constant is -79228162514264337593543950335.This field is read-only.
DecimalConstantAttribute(-79228162514264337593543950335, -79228162514264337593543950335, -79228162514264337593543950335, -79228162514264337593543950335, -79228162514264337593543950335)
System.Decimal Structure, System Namespace
public static readonly decimal MinusOne;
Contains negative one (-1).
This field is read-only.
DecimalConstantAttribute(-1, -1, -1, -1, -1)
System.Decimal Structure, System Namespace
public static readonly decimal One;
Contains one (1).
This field is read-only.
DecimalConstantAttribute(1, 1, 1, 1, 1)
System.Decimal Structure, System Namespace
public static readonly decimal Zero;
Contains zero (0).
This field is read-only.
DecimalConstantAttribute(0, 0, 0, 0, 0)
System.Decimal Structure, System Namespace