Math.round()取数法则

bhsxnlrl =(double) Math.round(bhsxnlrl * 100) / 100;
发现一句代码,感觉完全是脱裤子放屁的操作,出于好奇查了一下,嗯,发现了新大陆啊!
问Math.Round(11.5);和Math.Round(-11.5);返回值是多少?
结果是12 和 -11.
嗯,想了想,再看看别的数,然后。。。
Math.Round(3.4452, 2)=3.45
Math.Round(3.4552, 2)=3.46
Math.Round(3.4652, 2)=3.47
Math.Round(3.44, 1) = 3.4
Math.Round(3.45, 1) = 3.4
Math.Round(3.46, 1) = 3.5
Math.Round(3.54, 1) = 3.5
Math.Round(3.55, 1) = 3.6
Math.Round(3.56, 1) = 3.6
Math.Round(3.64, 1) = 3.6
Math.Round(3.65, 1) = 3.6
Math.Round(3.66, 1) = 3.7
Math.Round(3.74, 1) = 3.7
Math.Round(3.75, 1) = 3.8
Math.Round(3.76, 1) = 3.8
有点东西啊,这是bug啊!!!本着程序员的专业素养,必须解决bug啊,但是经过实验,坑爹啊!!!
后来又看到了一句话,是另一位博主总结的------
四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一!
膜拜了!!!活到老学到老!~

猜你喜欢

转载自blog.csdn.net/qq_40403688/article/details/87192101