|
Math.Round方法真正意義上的四舍五入 Math.Round四舍六入五成雙 不能直接調(diào)用Math.Round方法的,這可和Java的不一樣哦 Math.Round這個函數(shù)的解釋是將值按指定的小數(shù)位數(shù)舍入,并不就是四舍五入。這種舍入有時稱為就近舍入或四舍六入五成雙 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 函數(shù)都是采用 Banker's rounding(銀行家舍入)算法,即四舍六入五取偶。事實上這也是 IEEE 規(guī)定的舍入標準。因此所有符合 IEEE 標準的語言都應該是采用這一算法的。 請調(diào)用 Math.Round(Decimal, MidpointRounding) 重載!~哦,原來還有重載的方法可用,MidpointRounding在兩個數(shù)字之間時如何舍入的規(guī)范,規(guī)范MidpointRounding中它有2個成員,一個是ToEven還有個是AwayFromZero。 C# code //四舍五入 Math.Round(0.5,MidpointRounding.AwayFromZero) |
|
|