博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Converter -> public static int ToInt32(double value) 你用对了么?
阅读量:5064 次
发布时间:2019-06-12

本文共 3396 字,大约阅读时间需要 11 分钟。

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  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 upsome 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:

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

 

转载于:https://www.cnblogs.com/Dely/p/3791951.html

你可能感兴趣的文章
php变量什么情况下加大括号{}
查看>>
less入门
查看>>
如何实现手游app瘦身?
查看>>
linux程序设计---序
查看>>
OpenGL 笔记<1> 固定管线实例 + 双缓存测试实例
查看>>
【字符串入门专题1】hdu3613 【一个悲伤的exkmp】
查看>>
C# Linq获取两个List或数组的差集交集
查看>>
Django-Json 数据返回
查看>>
团队冲刺第六天个人博客
查看>>
使用Nginx实现灰度发布
查看>>
linux网口驱动实现(待续)
查看>>
github报错failed to push some refs to 'git
查看>>
21.Longest Palindromic Substring(最长回文子串)
查看>>
POJ 3177 Redundant Paths
查看>>
HDU 4635 Strongly connected
查看>>
testuse备份
查看>>
K近邻分类算法
查看>>
dsoFramer 的原代码
查看>>
PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
查看>>
DP Big Event in HDU
查看>>