Decimal.Round Method (Decimal, MidpointRounding)

 

Rounds a decimal value to the nearest integer. A parameter specifies how to round the value if it is midway between two other numbers.

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

Public Shared Function Round (
	d As Decimal,
	mode As MidpointRounding
) As Decimal

Parameters

d
Type: System.Decimal

A decimal number to round.

mode
Type: System.MidpointRounding

A value that specifies how to round d if it is midway between two other numbers.

Return Value

Type: System.Decimal

The integer that is nearest to the d parameter. If d is halfway between two numbers, one of which is even and the other odd, the mode parameter determines which of the two numbers is returned.

Exception Condition
ArgumentException

mode is not a System.MidpointRounding value.

OverflowException

The result is outside the range of a Decimal object.

In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 7.5 is a midpoint value if it is to be rounded to an integer. For midpoint values, the nearest value can't be easily identified without a rounding convention, which is specified by the mode argument. The Round method supports two rounding conventions for handling midpoint values.

Rounding away from zero.

Midpoint values are rounded to the next number away from zero. For example, 3.5 rounds to 4, 4.5 rounds to 5, -3.5 rounds to -4, and -4.5 rounds to -5. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

Rounding away from zero is the most widely known form of rounding.

Rounding to even, or banker's rounding

Midpoint values are rounded to the nearest even number. For example, both 3.5 and 4.5 round to 4, and both -3.5 and -4.5 round to -4. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.

The following example displays values returned by the Round(Decimal) method, the Round(Decimal, MidpointRounding) method with a mode argument of ToEven, and the Round(Decimal, MidpointRounding) method with a mode argument of AwayFromZero.

Module Example
   Public Sub Main()
      Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default", 
                        "ToEven", "AwayFromZero")
      For value As Decimal = 12.0d To 13.0d Step .1d
         Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
                           value, Math.Round(value), 
                           Math.Round(value, MidpointRounding.ToEven),
                           Math.Round(value, MidpointRounding.AwayFromZero))
      Next
   End Sub
End Module
' The example displays the following output:
'       Value      Default    ToEven     AwayFromZero
'       12         12         12         12
'       12.1       12         12         12
'       12.2       12         12         12
'       12.3       12         12         12
'       12.4       12         12         12
'       12.5       12         12         13
'       12.6       13         13         13
'       12.7       13         13         13
'       12.8       13         13         13
'       12.9       13         13         13
'       13.0       13         13         13

.NET Framework
Available since 2.0
Return to top
Show: