public struct Decimal: IComparable, IFormattable, IComparable<Decimal>, IEquatable<Decimal>
Object
ValueType
DecimalThis type implements IFormattable, IComparable, System.IComparable<System.Decimal>, and System.IEquatable<System.Decimal>.
mscorlib
ExtendedNumerics
Represents a floating-point decimal data type with at least 28 significant digits, suitable for financial and commercial calculations.
The Decimal type can represent values with at least 28 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 Decimal type shall represent values in at least the range -79228162514264337593543950335 to 79228162514264337593543950335, and having scale f such that 0 <= f <= at least 28.
A Decimal value occupies 128 bits; however, its representation is unspecified (see note below).
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 at least 28 digits, but not necessarily to more than 28 decimal places. A zero result has a sign 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).
The default initial value of an instance of type Decimal is zero with a scale of zero.
[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 can lose precision. If the value being converted is not within the range of the destination type, a OverflowException is thrown. A conversion from Single or Double to Decimal throws a OverflowException if the value is not within the range of the Decimal type.
]
[Note: Although different implementations of Decimal can have different representations, interchange of a decimal value within the range of the internal format can still be achieved by converting it to a string, exporting it, and then converting it back to internal format.
]
[Note: In Version 1 of this standard, the representation of Decimal was well-defined, as follows.
When considered as an array of four Int32s, it 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, as follows:
In order to allow alternate representations (such as in the update to the IEC floating-point standard, IEC-60559, currently in preparation), the representation has been made unspecified.
Bit Positions Name 0-15 (None.) 16-23 Scale 24-30 (None.) 31 Sign ]
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(System.Decimal) Method
Decimal.CompareTo(System.Object) Method
Decimal.Divide Method
Decimal.Equals(System.Decimal) 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.String) Method
Decimal.ToString() Method
Decimal.ToString(System.String, System.IFormatProvider) Method
Decimal.ToString(System.IFormatProvider) Method
Decimal.Truncate Method
Decimal.op_Addition Method
Decimal.op_Decrement Method
Decimal.op_Division Method
Decimal.op_Equality Method
double Decimal.op_Explicit(System.Decimal) Method
float Decimal.op_Explicit(System.Decimal) Method
ulong Decimal.op_Explicit(System.Decimal) Method
long Decimal.op_Explicit(System.Decimal) Method
uint Decimal.op_Explicit(System.Decimal) Method
int Decimal.op_Explicit(System.Decimal) Method
sbyte Decimal.op_Explicit(System.Decimal) Method
byte Decimal.op_Explicit(System.Decimal) Method
System.Decimal Decimal.op_Explicit(double) Method
System.Decimal Decimal.op_Explicit(float) Method
char Decimal.op_Explicit(System.Decimal) Method
ushort Decimal.op_Explicit(System.Decimal) Method
short 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. The scale of the new Decimal is 0.
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. The scale of the new Decimal is 0.
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. The scale of the new Decimal is 0.
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. The scale of the new Decimal is 0.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, but the Decimal representation does not support NaNs.
equal to System.Single.PositiveInfinity, but the Decimal representation does not support infinities.
equal to System.Single.NegativeInfinity, but the Decimal representation does not support infinities.
This constructor initializes the new Decimal to the value specified by value. This constructor rounds value to 7 significant digits using banker's rounding. The scale of the new Decimal is the same as that produced by System.Decimal.Parse(
value.ToString())
.
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, but the Decimal representation does not support NaNs.
equal to System.Double.PositiveInfinity, but the Decimal representation does not support infinities.
equal to System.Double.NegativeInfinity, but the Decimal representation does not support infinities.
This constructor initializes the new Decimal to the value specified by value. This constructor rounds value to 15 significant digits using banker's rounding. The scale of the new Decimal is the same as that produced by System.Decimal.Parse(
value.ToString())
.
System.Decimal Structure, System Namespace
public Decimal(int[] bits);
Constructs and initializes a new Decimal value.
- bits
- An array of four Int32 containing an unspecified 128-bit representation of a Decimal in its raw form.
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: A numeric value can 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.]
The format of bits is the same as that returned by System.Decimal.GetBits
(System.Decimal)
.
System.Decimal Structure, System Namespace
public static decimal Add(decimal d1, decimal d2);
Adds two Decimal values together.
- d1
- The first addend.
- d2
- The second addend.
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.
The return value is a negative number, zero, or a positive number reflecting the sort order of d1 as compared to d2. Trailing zero digits in the fractional part of are ignored. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value. Each comparison compares the numerical values of d1 and d2.
Return Value Meaning Any negative number d1< d2 Zero d1 == d2 A positive number d1>d2
System.Decimal Structure, System Namespace
public int CompareTo(decimal value);
Returns the sort order of the current instance compared to the specified Decimal.
- value
- The Decimal to compare to the current instance.
The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. Trailing zero digits in the fractional part of the current instance and value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:
Return Value Description A negative number Current instance < value. Zero Current instance == value. A positive number current instance > value.
[Note: This method is implemented to support the System.IComparable<Decimal> interface.]
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.
The return value is a negative number, zero, or a positive number reflecting the sort order of the current instance as compared to value. Trailing zero digits in the fractional part of the current instance and value. For non-zero return values, the exact value returned by this method is unspecified. The following table defines the return value:
Return Value Description A negative number Current instance < value. Zero Current instance == value. A 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 valuetype System.Decimal Divide(valuetype System.Decimal d1, valuetype System.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 closest scale to the preferred scale which will preserve a result equal to the exact result. The preferred scale is the scale of d1 less the scale of d2. For example, 2.20 / 2 gives 1.10.
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(decimal value);
Determines whether the current instance and the specified Decimal have the same value. Trailing zero digits in the fractional part are ignored.
- value
- The Decimal to compare to the current instance.
true
if value is numerically equal to (has the same value as) the current instance; otherwise,false
.
[Note: This method is implemented to support the System.IEquatable<Decimal> interface.]
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. Trailing zero digits in the fractional part are ignored.
- 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. Trailing zero digits in the fractional part are ignored.
- 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 an integer 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 four Int32 containing an unspecified 128-bit representation of a Decimal in its raw form.
The format of the bits in the 4-element array returned is the same as that used by the bits parameter to System.Decimal.Decimal(System.Int32)
.A numeric value can 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.
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); } }
System.Decimal Structure, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance. Trailing zero digits in the fractional part are ignored.
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 multiplier.
- d2
- The multiplicand.
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); otherwise the scale of the result is the same as the scale of d .
System.Decimal Structure, System Namespace
public static valuetype System.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. If the value is zero, the sign 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 valuetype System.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. If the value is zero, the sign 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 larger of the scales of d1 and 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(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 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, 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. "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 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 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 addend.
- d2
- The second addend.
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 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, and has a different radix than, Decimal.]
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, and has a different radix than, Decimal.]
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 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 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 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 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 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 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, but the Decimal representation does not support NaNs.
equal to System.Double.PositiveInfinity, but the Decimal representation does not support infinities.
equal to System.Double.NegativeInfinity, but the Decimal representation does not support infinities.
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, but the Decimal representation does not support NaNs.
equal to System.Single.PositiveInfinity, but the Decimal representation does not support infinities.
equal to System.Single.NegativeInfinity, but the Decimal representation does not support infinities.
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 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 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 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 multiplier.
- d2
- The multiplicand.
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 minuend.
- d2
- The subtrahend.
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 implementation-specific, but shall be greater than or equal to 79228162514264337593543950335. The scale shall be 0.This field is read-only.
DecimalConstantAttribute
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 implementation-specific, but shall be less than or equal to -79228162514264337593543950335. The scale shall be 0.This field is read-only.
DecimalConstantAttribute
System.Decimal Structure, System Namespace
public static readonly decimal MinusOne;
Contains negative one (-1).
The value of this constant is -1. The scale shall be 0.This field is read-only.
DecimalConstantAttribute
System.Decimal Structure, System Namespace
public static readonly decimal One;
Contains one (1).
The value of this constant is 1. The scale shall be 0.This field is read-only.
DecimalConstantAttribute
System.Decimal Structure, System Namespace
public static readonly decimal Zero;
Contains zero (0).
The value of this constant is 0. The scale shall be 0.This field is read-only.
DecimalConstantAttribute
System.Decimal Structure, System Namespace