使用英语阅读

通过


MidpointRounding 枚举

定义

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

C#
public enum MidpointRounding
C#
[System.Runtime.InteropServices.ComVisible(true)]
public enum MidpointRounding
继承
MidpointRounding
属性

字段

名称 说明
AwayFromZero 1

舍入到最接近的数字的策略,当一个数字介于两个数字之间的中间时,它会舍入到离零最近的数字。

ToEven 0

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

ToNegativeInfinity 3

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

ToPositiveInfinity 4

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

ToZero 2

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

示例

以下示例将 方法与 枚举一起MidpointRounding演示Math.Round

C#
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)
*/

注解

有关此 API 的详细信息,请参阅 MidpointRounding 的补充 API 说明

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0