Math.Round Method (Decimal, MidpointRounding)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two other numbers.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- d
- Type: System.Decimal
A decimal number to be rounded.
- mode
- Type: System.MidpointRounding
Specification for how to round d if it is midway between two other numbers.
Return Value
Type: System.DecimalThe integer nearest d. If d is halfway between two numbers, one of which is even and the other odd, then mode determines which of the two is returned.
| Exception | Condition |
|---|---|
| ArgumentException | mode is not a valid value of System.MidpointRounding. |
| OverflowException | The result is outside the range of a Decimal. |
The mode parameter controls how d is rounded if the fractional component of d is halfway between the one's digit and a value one greater than the one's digit. mode can have one of the following two values:
MidpointRounding.ToEven. If the one's digit is odd, it is changed to an even digit. Otherwise, it is left unchanged. This behavior follows IEEE Standard 754, section 4. It 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.
MidpointRounding.AwayFromZero. The one's digit is always rounded up to the next digit. This is the most commonly known rounding method. It is known as symmetric arithmetic rounding.
The following code example demonstrates how to use the Round method with the MidpointRounding enumeration.