MidpointRounding MidpointRounding MidpointRounding MidpointRounding Enum

定义

指定数学舍入方法应如何处理两个数字间的中间值。Specifies how mathematical rounding methods should process a number that is midway between two numbers.

public enum class MidpointRounding
[System.Runtime.InteropServices.ComVisible(true)]
public enum MidpointRounding
type MidpointRounding = 
Public Enum MidpointRounding
继承
MidpointRoundingMidpointRoundingMidpointRoundingMidpointRounding
属性

字段

AwayFromZero AwayFromZero AwayFromZero AwayFromZero 1

当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven ToEven ToEven ToEven 0

当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。When a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity ToNegativeInfinity 3
ToPositiveInfinity ToPositiveInfinity ToPositiveInfinity ToPositiveInfinity 4
ToZero ToZero ToZero ToZero 2

示例

下面的代码示例演示Round方法结合ToEven枚举。The following code example demonstrates the Round method in conjunction with the ToEven 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)

*/
decimal result = 0.0m;
decimal posValue =  3.45m;
decimal negValue = -3.45m;

// 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)

*/
' 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)
'

注解

使用MidpointRounding的相应重载Math.Round以提供更好地控制舍入的进程。Use MidpointRounding with appropriate overloads of Math.Round to provide more control of the rounding process.

舍入运算采用隐式或指定的精度; 原始数量检查下一个数字,位于该精度加 1;并返回最接近的数字与原始编号相同的精度。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. 对于正数和负数,如果下一个数字是从 0 到 4,最接近的数字是向负无穷。For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. 如果下一个数字是从 6 到 9,最接近的数字是向正无穷。If the next digit is from 6 through 9, the nearest number is toward positive infinity. 对于负数字,如果下一个数字是从 0 到 4,最接近的数字是向正无穷。For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. 如果下一个数字是从 6 到 9,最接近的数字是向负无穷。If the next digit is from 6 through 9, the nearest number is toward negative infinity.

在以前的情况下,MidpointRounding枚举不会影响舍入运算的结果。In the previous cases, the MidpointRounding enumeration does not affect the result of the rounding operation. 但是,如果一位数字等于 5,这是两个可能结果之间的中点和所有其余位均为零或没有任何剩余的数字,最接近的数字不明确。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. 在这种情况下,MidpointRounding枚举可用于指定舍入操作是否返回零个或最接近的偶数最接近的数字。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.

下表说明了结果的舍入的值与结合使用某些正负数MidpointRoundingThe 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. 例如,对于数字-2.5,小数点后的数字是 5。For example, for the number -2.5, the digit after the decimal point is 5. 由于此数字是的中点,您可以使用MidpointRounding值以确定结果的舍入。Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. 如果指定 AwayFromZero,则返回-3,因为它是最接近的数字零且精度为零。If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. 如果指定 ToEven,因为它是最接近的偶数且精度为零,则返回-2。If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

原始编号Original number AwayFromZeroAwayFromZero ToEvenToEven
3.53.5 44 44
2.82.8 33 33
2.52.5 33 22
2.12.1 22 22
-2.1-2.1 -2-2 -2-2
-2.5-2.5 -3-3 -2-2
-2.8-2.8 -3-3 -3-3
-3.5-3.5 -4-4 -4-4

适用于