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

MidpointRounding Enumeration

Note: This enumeration is new in the .NET Framework version 2.0.

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

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

[ComVisibleAttribute(true)] 
public enum class MidpointRounding
/** @attribute ComVisibleAttribute(true) */ 
public enum MidpointRounding
ComVisibleAttribute(true) 
public enum MidpointRounding
 Member nameDescription
AwayFromZeroWhen a number is halfway between two others, it is rounded toward the nearest number that is away from zero. 
ToEvenWhen a number is halfway between two others, it is rounded toward the nearest even number. 

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.
using namespace System;

void main()
{
    Decimal result = (Decimal) 0.0;
    Decimal posValue = (Decimal) 3.45;
    Decimal negValue = (Decimal) -3.45;

    // 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();
}

/*
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.

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.