通常我們使用Math.Round()函數,是直接調用的,如:
Math.Round(45.367,2) //返回: 45.37 Math.Round(45.365,2) //返回: 45.36
上面不是我們理解的四舍五入,是四舍六入,需要改成這樣,如:
Math.Round(Convert.ToDecimal(45.367),2,MidpointRounding.AwayFromZero); //返回: 45.37 Math.Round(Convert.ToDecimal(45.365),2,MidpointRounding.AwayFromZero); //返回: 45.37
PS: 上面需要我們先把值進行轉換成decimal類型,防止精度丟失(接收的變量是float或double會出現這個問題)
參考:
https://www.cnblogs.com/xuliangxing/p/6585865.html