在下面的代码段中,两种情况下我都希望达到13.70,但是第一位的期望值为13.69,第二位的期望值为13.70。
var d1 = Decimal.Round(13.694999999999999999999999998M, 2, MidpointRounding.AwayFromZero)
var d2 = Decimal.Round(13.695000M, 2, MidpointRounding.AwayFromZero);
答案 0 :(得分:5)
var d1 = Decimal.Round(13.694999999999999999999999998M, 2, MidpointRounding.AwayFromZero);
13.694999999999小于13.695,因此将舍入为13.69
MidpointRounding.AwayFromZero在这种情况下无效。
var d2 = Decimal.Round(13.695000M, 2, MidpointRounding.AwayFromZero);
13.695是恰好,在13.69和13.70之间,因此舍入取决于MidpointRounding.AwayFromZero,结果为13.70(远离0)
期望四舍五入后舍入较小的有效数字并使用这些值舍入较高的有效数字是错误的。例如:
将13.694999999999舍入到13.695,然后将13.695舍入到13.70