我有一个类似这样的东西:
double d1 = Math.Round(88.5, 0); // result 88
double d2 = Math.Round(89.5, 0); // result 90
为什么 Math.Round() 方法会把偶数向下舍入,奇数向上舍入?
我有一个类似这样的东西:
double d1 = Math.Round(88.5, 0); // result 88
double d2 = Math.Round(89.5, 0); // result 90
您可以在Math.Round中使用MidpointRounding参数。
当您使用Math.Round时,其中一个重载是接受2个参数的重载,第一个参数是您的值,第二个参数是MidpointRounding类型的枚举。
考虑下面的代码:
Math.Round(88.5, MidpointRounding.AwayFromZero) // The result is 89
Math.Round(88.5, MidpointRounding.ToEven) // The result is 88