【转】C#中Math.Round()实现中国式四舍五入

原文地址:https://www.cnblogs.com/xiaofengfeng/p/4729973.html

C#中的Math.Round()并不是使用的"四舍五入"法。其实在VB、VBScript、C#、J#、T-SQL中Round函数都是采用Banker's rounding(银行家算法),即:四舍六入五取偶。事实上这也是IEEE的规范,因此所有符合IEEE标准的语言都应该采用这样的算法。

.NET 2.0 开始,Math.Round 方法提供了一个枚举选项 MidpointRounding.AwayFromZero 可以用来实现传统意义上的"四舍五入"。即: Math.Round(4.5, MidpointRounding.AwayFromZero) = 5。

Round(Decimal)
Round(Double)
Round(Decimal, Int32)
Round(Decimal, MidpointRounding)
Round(Double, Int32)
Round(Double, MidpointRounding)
Round(Decimal, Int32, MidpointRounding)
Round(Double, Int32, MidpointRounding)

 如:

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(4.5) //result:4

使用MidpointRounding.AwayFromZero重载后对比:   

Math.Round(0.4, MidpointRounding.AwayFromZero); // result:0

Math.Round(0.6, MidpointRounding.AwayFromZero); // result:1

Math.Round(0.5, MidpointRounding.AwayFromZero); // result:1

Math.Round(1.5, MidpointRounding.AwayFromZero); // result:2

Math.Round(2.5, MidpointRounding.AwayFromZero); // result:3

Math.Round(3.5, MidpointRounding.AwayFromZero); // result:4

Math.Round(4.5, MidpointRounding.AwayFromZero); // result:5

 但是悲剧的是,如果用这个计算小数的话,就不灵了!!!

必须用第七个重载方法,

decimal Round(decimal d, int decimals, MidpointRounding mode)

这样计算出来的小数才是真正的中国式四舍五入!!

?Math.Round(526.925, 2)
526.92

?Math.Round(526.925, 2,MidpointRounding.AwayFromZero)
526.92

?Math.Round((decimal)526.925, 2)
526.92

?Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)
526.93

 

扩展

只入不舍

 /// <summary>
        /// 按小数位四舍五入
        /// </summary>
        /// <param name="value"></param>
        /// <param name="decimals"></param>
        /// <returns></returns>
        public static double ToRound(this double value, int decimals)
        {
            var c = Math.Pow(10, decimals);
            var s = Math.Truncate(value * c);
            if (s < value * c)
            {
                s += 1;
            }
            return Math.Round(s / 100, decimals);
        }

 

posted @ 2018-12-14 16:35  花影疏帘  阅读(340)  评论(0编辑  收藏  举报