四舍、六入、五凑偶之Math.Round()

来源:互联网 发布:违规网络音乐产品名单 编辑:程序博客网 时间:2024/06/02 10:28
Math.Round (Decimal, Int32)将小数值舍入到指定精度。
由 .NET Compact Framework 支持。Math.Round (Double, Int32)将双精度浮点值舍入到指定精度。
由 .NET Compact Framework 支持。


msdn并没有详细的说明清楚,给了一个例子;

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.445, 1); //Returns 3.4.
Math.Round(3.455, 1); //Returns 3.5.
Math.Round(3.465, 1); //Returns 3.5.

Math.Round(3.450, 1); //Returns 3.4.(补0是无效的)

Math.Round(3.4452, 2); //Returns 3.45.
Math.Round(3.4552, 2); //Returns 3.46.
Math.Round(3.4652, 2); //Returns 3.47.

世界上的许多国家已广泛采用“四舍六入法”。我国国家科委于1955年就作了推荐。“四舍 六入法”可以概括为:“四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍 去,五前为奇要进一。”Math.Round()方法使用的银行家算法的依据,是IEEE Standard 754这个国际标准。