Math.Round Method (Decimal, Int32)

July 28, 2014

Rounds a decimal value to a specified number of fractional digits.

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

public static decimal Round(
	decimal d,
	int decimals
)

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.Decimal
The number nearest d with a number of fractional digits equal to decimals.

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

The following example demonstrates rounding to nearest.


Math.Round(3.44, 1); //Returns 3.4.
Math.Round(3.45, 1); //Returns 3.4.
Math.Round(3.46, 1); //Returns 3.5.

Math.Round(4.34, 1); // Returns 4.3
Math.Round(4.35, 1); // Returns 4.4
Math.Round(4.36, 1); // Returns 4.4


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft