MidpointRounding 枚举

定义

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

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

字段

AwayFromZero 1

“舍入到最接近的值”模式:当一个数字是其他两个数字的中间值时,会将其舍入为两个值中绝对值较小的值。Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven 0

“舍入到最接近的值”模式:当一个数字是其他两个数字的中间值时,会将其舍入为最接近的偶数。Round to nearest mode: when a number is halfway between two others, it is rounded toward the nearest even number.

ToNegativeInfinity 3

“定向”模式:将数字向下舍入,并且结果最接近且不大于无限精确的结果。Directed mode: the number is rounded down, with the result closest to and no greater than the infinitely precise result.

ToPositiveInfinity 4

“定向”模式:将数字向上舍入,并且结果最接近且不小于无限精确的结果。Directed mode: the number is rounded up, with the result closest to and no less than the infinitely precise result.

ToZero 2

“定向”模式:将数字向零舍入,并且结果最接近且不大于无限精确的结果。Directed mode: the number is rounded toward zero, with the result closest to and no greater in magnitude than the infinitely precise result.

示例

下面的示例将 Math.Round 方法与枚举结合在一起 MidpointRoundingThe following example demonstrates the Math.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)

*/
decimal result = 0.0m;
decimal positiveValue = 3.45m;
decimal negativeValue = -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(positiveValue, 1);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1)");
result = Math.Round(negativeValue, 1);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1)");
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(positiveValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(positiveValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({positiveValue}, 1, MidpointRounding.AwayFromZero)");
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(negativeValue, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.ToEven)");
result = Math.Round(negativeValue, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({negativeValue}, 1, MidpointRounding.AwayFromZero)");
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.
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
End Class
'
'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.

舍入到最接近Round to nearest

舍入到最接近的操作采用具有隐式或指定精度的原始数字;检查下一个数字,该数字在该精度内加1。和返回与原始数字具有相同精度的最接近的数字。A round-to-nearest 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.AwayFromZeroMidpointRounding.ToEven 不会影响舍入操作的结果。In the previous cases, the MidpointRounding.AwayFromZero and MidpointRounding.ToEven do 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 round-to-nearest modes in MidpointRounding enable 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 round-to-nearest modes. 用于对数字进行舍入的精度为零,这意味着小数点之后的数字会影响舍入运算。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

定向舍入Directed rounding

定向舍入操作采用具有隐式或指定精度的原始数字,并返回与原始数字精度最接近的预定义值的下一个数字。A directed rounding operation takes an original number with an implicit or specified precision and returns the next number closest to some predefined one with the same precision as the original number. 定向模式 MidpointRounding 控制向其执行舍入的预定义数字。Directed modes on MidpointRounding control toward which predefined number the rounding is performed.

下表演示了与定向舍入模式结合的一些负数和正数的舍入结果。The following table demonstrates the results of rounding some negative and positive numbers in conjunction with directed rounding modes. 用于对数字进行舍入的精度为零,这意味着小数点前的数字会受舍入运算影响。The precision used to round the numbers is zero, which means the number before the decimal point is affected by the rounding operation.

原始数字Original number ToNegativeInfinityToNegativeInfinity ToPositiveInfinityToPositiveInfinity ToZeroToZero
2.82.8 22 33 22
2.52.5 22 33 22
2.12.1 22 33 22
-2.1-2.1 -3-3 -2-2 -2-2
-2.5-2.5 -3-3 -2-2 -2-2
-2.8-2.8 -3-3 -2-2 -2-2

适用于