C#中的Math.Round使用的"四舍五入"法
来源:互联网 发布:容声和美菱哪个好 知乎 编辑:程序博客网 时间:2024/05/17 03:24
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
Math.Round(5.5) //result:6
Math.Round(6.5) //result:6
Math.Round(7.5) //result:8
Math.Round(8.5) //result:8
Math.Round(9.5) //result:10
使用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
Math.Round(5.5, MidpointRounding.AwayFromZero); // result:6
Math.Round(6.5, MidpointRounding.AwayFromZero); // result:7
Math.Round(7.5, MidpointRounding.AwayFromZero); // result:8
Math.Round(8.5, MidpointRounding.AwayFromZero); // result:9
Math.Round(9.5, MidpointRounding.AwayFromZero); // result:10
- C#中的Math.Round使用的"四舍五入"法
- C#中Math.Round四舍五入的用法详解,
- Math.Round 不同的四舍五入
- Math.round()的四舍五入问题
- Math.round四舍五入的理解
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- java Math.round() Java中的四舍五入函数
- C# 中的四舍五入算法及 MSDN 2005 中文版 Math.Round 勘误
- C# 中的四舍五入算法及 MSDN 2005 中文版 Math.Round 勘误 .
- Math.Round()方法的四舍五入(.NET)
- java Math.round()比较特殊的四舍五入
- 最全IDC数据中心知识讲解(一)
- 字符串 比较 避免 null pointer exception
- sdut 2117 -数据结构实验之链表二:逆序建立链表
- tomcat无法启动 ClassNotFoundException org.springframework.web.context.ContextLoaderListener
- java mail发送邮件
- C#中的Math.Round使用的"四舍五入"法
- wxWidgets-3.0.2 Compile and Install (cc)
- 如何将vector 和 string 传递给 类C的API
- Centos服务器下网站做301重定向
- BPM制造业解决方案
- Ajax学习第一节_Ajax入门
- 令人头疼的java Tree整合bootstrap ace tree
- ioc和aop
- SpringMVC表单标签简介