MidpointRounding 枚举

定义

指定数学舍入方法应用于舍入数字的策略。

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

舍入到最接近的数字的策略,当数字在两个数字之间的中间时,它将舍入到离零的最接近的数字。

ToEven 0

舍入到最接近的数字的策略,当数字在两个数字之间的中间时,它将舍入到最接近的偶数。

ToNegativeInfinity 3

向下定向舍入的策略,结果最接近且不大于无限精确结果。

ToPositiveInfinity 4

向上定向舍入的策略,结果最接近且不小于无限精确结果。

ToZero 2

定向向零舍入的策略,结果最接近且数量级不大于无限精确结果。

示例

以下示例将演示如何 Math.Round 将方法与 MidpointRounding 枚举结合使用:

decimal result;

// Round a positive value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({3.47m}, 1, MidpointRounding.ToZero)\n");

// Round a negative value using different strategies.
// The precision of the result is 1 decimal place.

result = Math.Round(-3.45m, 1, MidpointRounding.ToEven);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.ToEven)");
result = Math.Round(-3.45m, 1, MidpointRounding.AwayFromZero);
Console.WriteLine($"{result} = Math.Round({-3.45m}, 1, MidpointRounding.AwayFromZero)");
result = Math.Round(-3.47m, 1, MidpointRounding.ToZero);
Console.WriteLine($"{result} = Math.Round({-3.47m}, 1, MidpointRounding.ToZero)\n");

/*
This code example produces the following results:

3.4 = Math.Round(3.45, 1, MidpointRounding.ToEven)
3.5 = Math.Round(3.45, 1, MidpointRounding.AwayFromZero)
3.4 = Math.Round(3.47, 1, MidpointRounding.ToZero)

-3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven)
-3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
-3.4 = Math.Round(-3.47, 1, MidpointRounding.ToZero)
*/
Dim result As Decimal = 0D
Dim posValue As Decimal = 3.45D
Dim negValue As Decimal = -3.45D

' Round a positive value using different strategies.
' 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)
result = Math.Round(posValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, posValue)
Console.WriteLine()

' Round a negative value using different strategies.
' 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)
result = Math.Round(negValue, 1, MidpointRounding.ToZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToZero)",
                   result, negValue)
Console.WriteLine()

'This code example produces the following results:
'
'        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.ToZero)
'
'        -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.ToZero)
'

注解

与适当的重载Math.Round一起使用MidpointRoundingMathF.Round并提供Decimal.Round对舍入过程的更多控制。

有两个整体舍入策略,舍入到最接近和定向舍入,每个枚举字段只参与其中一个策略。

舍入到最接近

字段:

舍入到最接近的操作采用具有隐式或指定精度的原始数字;检查下一位数字,该数字的精度为 1;并返回与原始数字相同的最接近的数字。 对于正数,如果下一位数字从 0 到 4,则最接近的数字为负无穷大。 如果下一位数字从 6 到 9,则最接近的数字是正无穷大。 对于负数,如果下一个数字从 0 到 4,则最接近的数字为正无穷大。 如果下一位数字从 6 到 9,则最接近的数字为负无穷大。

如果下一位数字从 0 到 4 或 6 到 9,则 MidpointRounding.AwayFromZero 不会影响 MidpointRounding.ToEven 舍入操作的结果。 但是,如果下一位数字为 5,这是两个可能结果之间的中点,并且所有剩余的数字均为零或没有剩余数字,则最接近的数字不明确。 在这种情况下,通过舍入到最接近的模式 MidpointRounding ,可以指定舍入操作是返回最接近的从零还是最接近的偶数。

下表演示了将一些负数和正数舍入的结果以及舍入到最接近的模式。 用于对数字进行舍入的精度为零,这意味着小数点后的数字会影响舍入运算。 例如,对于数字 -2.5,小数点后面的数字为 5。 由于该数字是中点,因此可以使用值 MidpointRounding 来确定舍入结果。 如果 AwayFromZero 指定,则返回 -3,因为它是离零最近的数字,精度为零。 如果 ToEven 指定,则返回 -2,因为它是精度为零的最接近偶数。

原始数字 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

定向舍入

字段:

定向舍入运算采用具有隐式或指定精度的原始数字,并在与原始数字相同的特定方向返回下一个最接近的数字。 控制 MidpointRounding 执行舍入的预定义编号的定向模式。

下表演示了将一些负数和正数舍入与定向舍入模式的结果。 用于舍入数字的精度为零,这意味着小数点前的数字受舍入运算的影响。

原始数字 ToNegativeInfinity ToPositiveInfinity ToZero
3.5 3 4 3
2.8 2 3 2
2.5 2 3 2
2.1 2 3 2
-2.1 -3 -2 -2
-2.5 -3 -2 -2
-2.8 -3 -2 -2
-3.5 -4 -3 -3

适用于