Math.Round Method (Decimal, MidpointRounding)

July 28, 2014

Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two other numbers.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static decimal Round(
	decimal d,
	MidpointRounding mode
)

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.Decimal
The 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.

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

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2014 Microsoft