发现Math.Round的一个问题。不知谁能告诉我,微软这么做这个函数,意义何在?
发觉微软的Math.Round,并非我们所想的四舍五入法。(也并非五舍六入)
--微软MSDN Libary 中是这么介绍的.
Math.Round 方法
返回最接近指定值的数字。
示例
下面的代码演示就近舍入。
[Visual Basic]
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.
就看他的示例,还以为是五舍六入呢.
但测试发现是这样子的
Math.Round(3.05, 1) 'Returns 3.0.
Math.Round(3.15, 1) 'Returns 3.2.
Math.Round(3.25, 1) 'Returns 3.2.
Math.Round(3.35, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.55, 1) 'Returns 3.6.
Math.Round(3.65, 1) 'Returns 3.6.
Math.Round(3.75, 1) 'Returns 3.8.
Math.Round(3.85, 1) 'Returns 3.8.
Math.Round(3.95, 1) 'Returns 4.0.
后来查MSDN Libary中的Decimal.Round 方法中见到。
备注
当 d 正好位于两个舍入值的正中间时,结果将是最右边小数位中有偶数位的舍入值。例如,当舍入为两位小数时,值 2.345 变成 2.34,而值 2.355 变成 2.36。此过程称为向偶舍入或就近舍入。
不知谁能告诉我,微软这么做这个函数,意义何在?