MidpointRounding Enumeration
Assembly: mscorlib (in mscorlib.dll)
Use MidpointRounding with appropriate overloads of System.Math.Round to provide more control in the rounding process.
It is ambiguous how a rounding method should handle a number that is at the midpoint between two numbers. For example, the number 2.5 is at the midpoint between 2 and 3. MidpointRounding specifies whether rounding should be toward the even number, which yields 2 in this example, or away from zero, which yields 3.
The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of MidpointRounding:
| Original number | AwayFromZero | ToEven |
|---|---|---|
| 3.5 | 4 | 4 |
| 2.8 | 3 | 3 |
| 2.5 | 3 | 2 |
| 2.1 | 2 | 2 |
| -2.1 | -2 | -2 |
| -2.5 | -3 | -2 |
| -2.8 | -3 | -3 |
| -3.5 | -4 | -4 |
The following code example demonstrates the Round method in conjunction with the MidpointRounding enumeration.
' This example demonstrates the Math.Round() method in conjunction ' with the MidpointRounding enumeration. Imports System Class Sample Public Shared Sub Main() Dim result As Decimal = 0D Dim posValue As Decimal = 3.45D Dim negValue As Decimal = -3.45D ' By default, round a positive and a negative value to the nearest even number. ' The precision of the result is 1 decimal place. result = Math.Round(posValue, 1) Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue) result = Math.Round(negValue, 1) Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue) Console.WriteLine() ' Round a positive value to the nearest even number, then to the nearest number ' away from zero. The precision of the result is 1 decimal place. result = Math.Round(posValue, 1, MidpointRounding.ToEven) Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _ result, posValue) result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero) Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _ result, posValue) Console.WriteLine() ' Round a negative value to the nearest even number, then to the nearest number ' away from zero. The precision of the result is 1 decimal place. result = Math.Round(negValue, 1, MidpointRounding.ToEven) Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _ result, negValue) result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero) Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _ result, negValue) Console.WriteLine() End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' ' 3.4 = Math.Round( 3.45, 1) '-3.4 = Math.Round(-3.45, 1) ' ' 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven) ' 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero) ' '-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven) '-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero) '
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.