Converter -> public static int ToInt32(double value) 你用对了么?
Convert.ToInt32() 是我们经常使用的方法,但如果我们写如下的代码,能确定它的输出值么?
1 var x = 7.5; 2 Console.WriteLine(7.5 + ": " + Convert.ToInt32(x)); 3 x = 6.5; 4 Console.WriteLine(6.5 + ": " + Convert.ToInt32(x));
让我意外的是,它的输出为:
7.5: 8
6.5: 6
有点疑惑,所以用Reflector看下了代码:
1 [__DynamicallyInvokable] 2 public static int ToInt32(double value) 3 { 4 if (value >= 0.0) 5 { 6 if (value < 2147483647.5) 7 { 8 int num = (int) value; 9 double num2 = value - num; 10 if ((num2 > 0.5) || ((num2 == 0.5) && ((num & 1) != 0))) 11 { 12 num++; 13 } 14 return num; 15 } 16 } 17 else if (value >= -2147483648.5) 18 { 19 int num3 = (int) value; 20 double num4 = value - num3; 21 if ((num4 < -0.5) || ((num4 == -0.5) && ((num3 & 1) != 0))) 22 { 23 num3--; 24 } 25 return num3; 26 } 27 throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); 28 } 29 30 31 32 33 1 0x1[__DynamicallyInvokable] 34 public static int ToInt32(double value) 35 { 36 if (value >= 0.0) 37 { 38 if (value < 2147483647.5) 39 { 40 int num = (int) value; 41 double num2 = value - num; 42 if ((num2 > 0.5) || ((num2 == 0.5) && ((num & 1) != 0))) 43 { 44 num++; 45 } 46 return num; 47 } 48 } 49 else if (value >= -2147483648.5) 50 { 51 int num3 = (int) value; 52 double num4 = value - num3; 53 if ((num4 < -0.5) || ((num4 == -0.5) && ((num3 & 1) != 0))) 54 { 55 num3--; 56 } 57 return num3; 58 } 59 throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); 60 } 61 62
6-15行的处理说明了问题,((num2 == 0.5) && ((num & 1) != 0) 判断如果是奇数,那么结果自增,否则不处理。Why? 为什么要这么处理? google了一下,发现.net的四舍五入都会使用“银行家四舍五入算法”:
The History section of the Wikipedia entry for Rounding has some statements about the role of "round to even" in computing. Interestingly, it appears "Bankers Rounding" has little evidence to state it was official in any sense of the word, so can only be chalked up as slang terminology.
It is only "more logical" if you subscribe to that rounding mechanism. Bankers rounding (which is the default in this case) is also perfectly logical.
Imagine if banks rounded up to the nearest penny for every fractional amount, they would make a lot less (lose a lot of, for the cynical) money with the millions upon millions of transactions that are processed daily. OK, so this example is cynical.
Going towards the nearest even number (or odd, but history chose otherwise) means that not every rounding resolution goes up, some can now go down. When you put this to the law of averages, it becomes a fair solution to use when considering who is responsible for paying for the extra half penny.
As for why this was chosen for the framework, this question attempts to address it:
Why does .NET use banker's rounding as default?
Of course, this all harks back to financial days and its applicability to integral numbers could be questioned, but why bother? Accept it, override it if you want to, just understand how it works.
请参阅:
http://stackoverflow.com/questions/11431793/convert-toint32-rounds-to-the-nearest-even-number
http://stackoverflow.com/questions/311696/why-does-net-use-bankers-rounding-as-default