C# 中沒有四舍五入函數(shù),程序語言都沒有四舍五入函數(shù),因為四舍五入算法不科學,國際通行的是 Banker 舍入法
Bankers rounding(銀行家舍入)算法,即四舍六入五取偶。事實上這也是 IEEE 規(guī)定的舍入標準。因此所有符合 IEEE 標準的語言都應該是采用這一算法的。
Math.Round 方法默認的也是 Banker 舍入法
在 .NET 2.0 中 Math.Round 方法有幾個重載方法
Math.Round(Decimal, MidpointRounding)Math.Round(Double, MidpointRounding)Math.Round(Decimal, Int32, MidpointRounding)Math.Round(Double, Int32, MidpointRounding)
將小數(shù)值舍入到指定精度。MidpointRounding 參數(shù),指定當一個值正好處于另兩個數(shù)中間時如何舍入這個值
該參數(shù)是個 MidpointRounding 枚舉
此枚舉有兩個成員,MSDN 中的說明是:
AwayFromZero 當一個數(shù)字是其他兩個數(shù)字的中間值時,會將其舍入為兩個值中絕對值較小的值。
ToEven 當一個數(shù)字是其他兩個數(shù)字的中間值時,會將其舍入為最接近的偶數(shù)。
注 意!這里關(guān)于 MidpointRounding.AwayFromZero 的說明是錯誤的!實際舍入為兩個值中絕對值較大的值。不過 MSDN 中的 例子是正確的,英文描述原文是 it is rounded toward the nearest number that is away from zero.
所以,要實現(xiàn)四舍五入函數(shù),對于正數(shù),可以加一個 MidpointRounding.AwayFromZero 參數(shù)指定當一個數(shù)字是其他兩個數(shù)字的中間值時其舍入為兩個值中絕對值較大的值,例:
Math.Round(3.45, 2, MidpointRounding.AwayFromZero)
不過對于負數(shù)上面的方法就又不對了
因此需要自己寫個函數(shù)來處理
第一個函數(shù):
double Round(double value, int decimals){ if (value < 0) { return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero); } else { return Math.Round(value, decimals, MidpointRounding.AwayFromZero); }}
第二個函數(shù):
double Round(double d, int i){ if(d >=0) { d += 5 * Math.Pow(10, -(i + 1)); } else { d += -5 * Math.Pow(10, -(i + 1)); } string str = d.ToString(); string[] strs = str.Split('.'); int idot = str.IndexOf('.'); string prestr = strs[0]; string poststr = strs[1]; if(poststr.Length > i) { poststr = str.Substring(idot + 1, i); } string strd = prestr + "." + poststr; d = Double.Parse(strd); return d;}
參數(shù):d表示要四舍五入的數(shù);i表示要保留的小數(shù)點后為數(shù)。
其中第二種方法是正負數(shù)都四舍五入,第一種方法是正數(shù)四舍五入,負數(shù)是五舍六入。
備注:個人認為第一種方法適合處理貨幣計算,而第二種方法適合數(shù)據(jù)統(tǒng)計的顯示。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答