MidpointRounding 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定数学舍入方法应用于对数字进行舍入的策略。
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
- 继承
- 属性
字段
| 名称 | 值 | 说明 |
|---|---|---|
| ToEven | 0 | 舍入到最接近的数字的策略。 当一个数字在另外两个数字中间时,它会舍入到最接近的偶数。 非中点值不受此选项影响,并且舍入为最接近的可表示值。 也称为银行家的舍入。 |
| AwayFromZero | 1 | 舍入到最接近的数字的策略。 当一个数字在另外两个数字之间中间时,它会舍入到离零最近的数字。 非中点值不受此选项影响,并且舍入为最接近的可表示值。 |
| ToZero | 2 | 定向舍入到零的策略,其中每个值(而不仅仅是中点)都向零舍入。 结果是最接近且数量级不超过无限精确结果的值。 舍入为零位数时,这相当于截断;当舍入为非零位数时,它会在指定的精度处截断为零。 |
| ToNegativeInfinity | 3 | 向下定向舍入的策略,其中每个值(而不仅仅是中点)都向负无穷大舍入。 结果是最接近且不大于无限精确结果的值。 当舍入为零位数字时,这相当于数学底函数;当舍入为非零位数的数字时,它会以指定的精度应用相同的行为。 |
| ToPositiveInfinity | 4 | 向上定向舍入的策略,其中每个值(而不仅仅是中点)都向正无穷大舍入。 结果是最接近且不小于无限精确结果的值。 舍入为零位数字时,这相当于数学上限函数;当舍入为非零位数的数字时,它会以指定的精度应用相同的行为。 |
示例
以下示例演示 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)
'
注解
将 MidpointRounding 枚举与适当的 Math.Round、MathF.Round 和 Decimal.Round 重载结合使用,以便对舍入过程提供更多控制。
尽管枚举的名称,但并不是每个模式都只是关于如何处理中点。 MidpointRounding 定义两个整体舍入策略(舍入到最接近 和 定向舍入),每个枚举字段只参与以下策略之一:
- 舍入到最接近的模式 (AwayFromZero和ToEven) 始终舍入到最接近的可表示值。 仅当输入在两个值( 中点)之间正好中间时,它们才彼此不同,这就是枚举的名称所引用的内容。
- 定向舍入模式(ToNegativeInfinity和ToPositiveInfinityToZero)始终以固定方向舍入每个值(而不仅仅是中点)。 即使输入不是中点,结果也是该方向最接近的可表示值。
四舍五入到最接近的值
Fields:
四舍五入操作将采用具有隐式或指定精度的原始数字;检查位于该精度加一位的下一位数字,并返回与原始数字具有相同精度的最接近数字。 对于正数,如果下一位数字在 0 到 4 范围内,则最接近的数字是负无穷大。 如果下一位数字是在 6 到 9 范围内,则最接近的数字是正无穷大。 对于负数,如果下一位数字在 0 到 4 范围内,则最接近的数字是正无穷大。 如果下一位数字是在 6 到 9 范围内,则最接近的数字是负无穷大。
如果下一位数字从 0 到 4 或 6 到 9,则选择 AwayFromZero 并 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 |
定向舍入
Fields:
定向舍入运算采用具有隐式或指定精度的原始数字,并返回特定方向上具有与原始数字相同精度的下一个最接近的数字。 控制每个值的舍入方向的定向模式 MidpointRounding 。 与最接近的模式不同,这些模式适用于每个输入值,而不仅仅是中点;例如, ToPositiveInfinity 将 2.1、2.5 和 2.8 舍入到 3。
下表演示了结合定向舍入模式对一些负数和正数进行舍入的结果。 用于对数字进行舍入的精度为零,这意味着小数点前的数字受舍入运算的影响。
| 原始数字 | 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 |