傳統「四捨五入」在大量數據累加時,會導致結果系統性偏大(因 5 總是進位)。統計學證明,「銀行家圍入法」(Banker's Rounding,即 **四捨六入五成雙**,中文常稱「奇進偶不進」)能使圍入誤差均值趨近零,避免誤差累積,是金融、科學計算的標準圍入規則。
MidpointRounding.ToEvendouble d = 5.215; double res = Math.Round(d, 2, MidpointRounding.ToEven); Console.WriteLine(res); // 5.22 d = 5.225; res = Math.Round(d, 2, MidpointRounding.ToEven); Console.WriteLine(res); // 5.22
// 4 double d = 5.214; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 5.21 // 6 d = 5.216; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 5.22 // 5 - 奇進 d = 5.215; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 5.22 // 5 - 偶不進 d = 5.225; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 5.22 // 更多小數位 d = 0.7865666; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 0.79 d = 0.786; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 0.79 d = 0.785; Console.WriteLine(Math.Round(d, 2, MidpointRounding.ToEven)); // 0.78
MidpointRounding.AwayFromZero(傳統四捨五入)Math.Round(5.225, 2, MidpointRounding.AwayFromZero); // 5.23(總是進位) Math.Round(5.215, 2, MidpointRounding.AwayFromZero); // 5.22
C# 通過 Math.Round(value, digits, MidpointRounding.ToEven) 直接支持銀行家圍入法,無需手動實現。正確選擇圍入模式對數據準確性至關重要,**金融、統計場景請優先使用 ToEven**,除非業務明確要求傳統四捨五入。