Round Method (Decimal, Int32, MidpointRounding)

Math.Round Method (Decimal, Int32, 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 a specified number of fractional digits. 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,
	int decimals,
	MidpointRounding mode
)

Parameters

d
Type: System.Decimal
A decimal number to be rounded.
decimals
Type: System.Int32
The number of decimal places in the return value.
mode
Type: System.MidpointRounding
Specification for how to round d if it is midway between two other numbers.

Return Value

Type: System.Decimal
The number nearest to d that contains a number of fractional digits equal to decimals. If d has fewer fractional digits than decimals, d is returned unchanged.

ExceptionCondition
ArgumentOutOfRangeException

decimals is less than 0 or greater than 28.

ArgumentException

mode is not a valid value of System.MidpointRounding.

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.

The mode parameter controls how d is rounded if the value to the right of the decimals position is halfway between the x and x+1, where x represents the value in the decimals position. mode can have one of the following two values:

  • MidpointRounding.ToEven. If the digit in the decimals position 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.

  • AwayFromZero. The digit in the decimals position 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:
© 2017 Microsoft