
正文
C#中Math.Round()的中国式用法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
C#中的Math.Round()并不是使用的"四舍五入"法。而是(银行家算法),即:四舍六入五取偶。事实上这也是IEEE的规范,因此所有符合IEEE标准的语言都应该采用这样的算法。
.NET 2.0 开始,Math.Round 方法提供了一个枚举选项 MidpointRounding.AwayFromZero 可以实现传统意义上的"四舍五入"。即: Math.Round(4.5, MidpointRounding.AwayFromZero) = 5。
如:Math.Round(0.4) //result:0Math.Round(0.6) //result:1Math.Round(0.5) //result:0Math.Round(1.5) //result:2Math.Round(2.5) //result:2Math.Round(3.5) //result:4Math.Round(4.5) //result:4Math.Round(5.5) //result:6Math.Round(6.5) //result:6Math.Round(7.5) //result:8Math.Round(8.5) //result:8Math.Round(9.5) //result:10 使用MidpointRounding.AwayFromZero重载后对比: Math.Round(0.4, MidpointRounding.AwayFromZero); // result:0Math.Round(0.6, MidpointRounding.AwayFromZero); // result:1Math.Round(0.5, MidpointRounding.AwayFromZero); // result:1Math.Round(1.5, MidpointRounding.AwayFromZero); // result:2Math.Round(2.5, MidpointRounding.AwayFromZero); // result:3Math.Round(3.5, MidpointRounding.AwayFromZero); // result:4Math.Round(4.5, MidpointRounding.AwayFromZero); // result:5Math.Round(5.5, MidpointRounding.AwayFromZero); // result:6Math.Round(6.5, MidpointRounding.AwayFromZero); // result:7Math.Round(7.5, MidpointRounding.AwayFromZero); // result:8Math.Round(8.5, MidpointRounding.AwayFromZero); // result:9Math.Round(9.5, MidpointRounding.AwayFromZero); // result:10
?Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)
结果是:
526.93今天就写到这,跟大家扯点别的,在现在这个社会中爱情的规则就是

你们觉得对吗?仔细想想身边的人,是不是这样呢?








