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.

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

Syntax

Public Shared Function Round ( _
    d As Decimal, _
    decimals As Integer _
) As Decimal
public static decimal Round(
    decimal d,
    int decimals
)

Parameters

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

Exceptions

Exception Condition
ArgumentOutOfRangeException

decimals is less than 0 or greater than 28.

OverflowException

The result is outside the range of a Decimal.

Remarks

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.

Examples

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

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Math Class

Round Overload

System Namespace

Ceiling

Floor