天天看點

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

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這個國際标準。

繼續閱讀