Export (0) Print
Expand All
Expand Minimize
3 out of 9 rated this helpful - Rate this topic

MidpointRounding Enumeration

Specifies how mathematical rounding methods should process a number that is midway between two numbers.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[<ComVisibleAttribute(true)>]
type MidpointRounding
Member nameDescription
Supported by Portable Class LibrarySupported in .NET for Windows Store appsAwayFromZeroWhen a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
Supported by Portable Class LibrarySupported in .NET for Windows Store appsToEvenWhen a number is halfway between two others, it is rounded toward the nearest even number.

Use MidpointRounding with appropriate overloads of Math.Round to provide more control of the rounding process.

A rounding operation takes an original number with an implicit or specified precision; examines the next digit, which is at that precision plus one; and returns the nearest number with the same precision as the original number. For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. If the next digit is from 6 through 9, the nearest number is toward positive infinity. For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. If the next digit is from 6 through 9, the nearest number is toward negative infinity.

In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation. However, if the next digit is 5, which is the midpoint between two possible results, and all remaining digits are zero or there are no remaining digits, the nearest number is ambiguous. In this case, the MidpointRounding enumeration enables you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with the values of MidpointRounding. The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. For example, for the number -2.5, the digit after the decimal point is 5. Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.