Math.Round Method (Decimal, Int32)

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 to d that contains 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.

This method is equivalent to calling the Round method with a mode argument of MidpointRounding.ToEven. If the value to the right of the decimals position is halfway between x and x+1, where x represents the value in the decimals position, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If there is a single non-zero digit in d to the right of the decimals decimal position and its value is 5, the digit in the decimals position is rounded up if it is odd, or left unchanged if it is even. If d has the same number or fewer fractional digits than decimals, d is returned unchanged.

For example, if decimals is equal to 1, 2.15 and 2.15000 are both rounded to 2.2 because .05 and .05000 are both halfway between .1 and .2, and .1 is odd. Similarly, if decimals is equal to 1, 2.05 and 2.05000 are both rounded to 2.0 because .05 and .05000 are both halfway between .0 and .1, and .0 is even.

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.

To control the type of rounding used by the Round(Decimal, Int32) method, call the Decimal.Round(Decimal, Int32, MidpointRounding) overload.

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft