Math.Round方法真正意义上的四舍五入

  • 格式:docx
  • 大小:14.50 KB
  • 文档页数:1

Math.Round方法真正意义上的四舍五入
不能直接调用Math.Round方法的,这可和Java的不一样哦
Math.Round这个函数的解释是将值按指定的小数位数舍入,并不就是四舍五入。

这种舍入有时称为就近舍入或四舍六入五成双
C# code
Math.Round(0.4) //result:0
Math.Round(0.6) //result:1
Math.Round(0.5) //result:0
Math.Round(1.5) //result:2
Math.Round(2.5) //result:2
Math.Round(3.5) //result:4
Math.Round(5.5) //result:6
Math.Round(6.5) //result:6
Math.Round(8.5) //result:8
Math.Round(9.5) //result:10
可以看出并不是四舍五入的
其实在VB, VBScript, C#, J#, T-SQL 中Round 函数都是采用Banker's rounding(银行家舍入)算法,即四舍六入五取偶。

事实上这也是IEEE 规定的舍入标准。

因此所有符合I EEE 标准的语言都应该是采用这一算法的。

请调用 Math.Round(Decimal, MidpointRounding) 重载!~哦,原来还有重载的方法可用,MidpointRounding在两个数字之间时如何舍入的规范,规范MidpointRounding中它有2个成员,一个是ToEven还有个是AwayFromZero。

C# code
//四舍五入 Math.Round(0.5,MidpointRounding.AwayFromZero)。