Partager via


Math.Round Method (Decimal, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

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

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.