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: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
?Math.Round((decimal)526.925, 2,MidpointRounding.AwayFromZero)
结果是:
526.93 今天就写到这,跟大家扯点别的,在现在这个社会中爱情的规则就是

你们觉得对吗?仔细想想身边的人,是不是这样呢?
C#中Math.Round()的中国式用法的更多相关文章
- C#中Math.Round()实现中国式四舍五入
C#中Math.Round()实现中国式四舍五入 C#中的Math.Round()并不是使用的"四舍五入"法.其实在VB.VBScript.C#.J#.T-SQL中Round函数都 ...
- C#中Math.Round()实现中国式四舍五入问题
C#中的Math.Round()并不是使用的"四舍五入"法.实际上是四舍六入五取偶. 对于这个问题我们常见的解决方式是使用MidpointRounding.AwayFromZero ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)
js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结
以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】
[摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...
- js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结
Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...
- .Net中Math.Round与四舍五入
有不少人误将Math.Round函数当作四舍五入函数在处理, 结果往往不正确, 实际上Math.Round采用的是国际通行的是 Banker 舍入法. Banker's rounding(银行家舍入) ...
- Java中Math.round()函数
Math.round(11.5) = 12; Math.round(-11.5) = -11; Math.round()函数是求某个数的整数部分,且四舍五入.
- C# Math.Round实现中国式四舍五入
decimal sum = 11111.334; sum = , MidpointRounding.AwayFromZero); sum:11111.33decimal sum = 11111.34 ...
随机推荐
- HTTP和HTTPS的区别?
HTTP1.1(Hypertext Transfer Protocol Vertion 1.1)超文本传输协议-版本1.1它是用来在Internet上传送超文本的传送协议.它是运行在Tcp/Ip协议族 ...
- OLEDB存取BLOB型数据
现代数据库系统除了支持一些标准的通用数据类型以外,大多数还支持一种称之为BLOB型的数据. BLOB全称为big large object bytes, 大二进制对象类型,这种类型的数据通常用于存储文 ...
- BZOJ3033: 太鼓达人(欧拉回路)
题意 题目链接 Sol 第一问的答案是\(2^M\),因为每个位置只有\(0 / 1\)两种情况,最优情况下一定是每个位置代表着一个长度为\(K\)的字符串 考虑相邻两个字符串之间的转化,第二个字符串 ...
- 从list中随机选出几个数,并按照原来的顺序排列
需求: 从list中随机选出几个数,并按照原来的顺序排列(比如从list中随机选出6个数) 方案一: //若对象size大于6,则随机去除6个对象,并按照原来的顺序排列 while(list.size ...
- rem,em,px
http://pxtoem.com/ px,em,rem单位转换工具 px(Pixel)根据显示屏分辨率,固定大小 em,根据父元素大小进行改变 rem,根据html进行相对改变 em 1. bod ...
- github使用手册
1.git init 2.git add README.md (增加文件夹/文件:git add dir/files) 3.git commit -m "注释内容” 4.git push - ...
- css 条纹背景
先介绍文章用到的二个知识点 background-size 属性 语法 background-size: length|percentage|cover|contain; css线性渐变 linear ...
- Elipse plugin or Bundle & OSGI
Develop and register service, lookup and use service! Android Design on service's publish-find-bind ...
- bit_count
bit_count函数的含义 用来计算二进制数中包含1的个数. select BIT_COUNT(10); 因为10转成二进制是1010,所以该结果就是2. bit_or函数的含义 就是对两个二进制数 ...
- maven---Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4
Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 Failure to transfer or ...