Math.Round Method (Decimal, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Rounds a decimal value to a specified number of fractional digits.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- d
- Type: System.Decimal
A decimal number to be rounded.
- decimals
- Type: System.Int32
The number of decimal places in the return value.
Return Value
Type: System.DecimalThe number nearest d with a number of fractional digits equal to decimals.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | decimals is less than 0 or greater than 28. |
| OverflowException | The result is outside the range of a Decimal. |
The decimals parameter specifies the number of fractional digits in the return value and ranges from 0 to 28. If decimals is zero, an integer is returned.
If the value of the digits in d to the right of the decimals position is halfway between the digit in position decimals, that digit is rounded up if it is odd, or left unchanged if it is even. If the precision of d is less than decimals, d is returned unchanged.
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.