你实际上不在中点。 MidpointRounding.ToEven
表示如果您有号码99.965,即99.96500000 [等],,然后,您将得到99.96。由于您传递给Math.Round的数字高于此中点,因此它正在四舍五入。
如果你希望你的数字向下舍入到99.96,做到这一点:
// this will round 99.965 down to 99.96
return Math.Round(Math.Truncate(99.96535789*1000)/1000, 2, MidpointRounding.ToEven);
哎,这里有一个方便的小功能上面做了一般的情况:
// This is meant to be cute;
// I take no responsibility for floating-point errors.
double TruncateThenRound(double value, int digits, MidpointRounding mode) {
double multiplier = Math.Pow(10.0, digits + 1);
double truncated = Math.Truncate(value * multiplier)/multiplier;
return Math.Round(truncated, digits, mode);
}
所以你说的是,你想要它___到一个数字是_远离_?这是“四舍五入”的一个奇怪的定义。 – 2009-09-14 18:13:25
这就是银行家对你的四舍五入。难怪我们在这个混乱;-) – 2009-09-14 18:26:14
不,这不是银行家的四舍五入。银行家的四舍五入是当你选择四舍五入时,当两个选择同样遥远*。你不处于两种选择同样遥远的情况。其中一个比另一个更近,而你想选择更远的那个。 – 2009-09-14 22:35:46