Math.round()什么作用?
2015-08-24 11:01
411 查看
题目大概是问Math.Round(11.5);和Math.Round(-11.5);返回值是多少?
原来不是四舍五入这么简单,其实是四舍六入,但是五是很有讲究的。要是遇到五则取离前一位最近的偶数。比如,这条题目,11.5返回值就是12.0;-11.5返回值就是-12.0;
但是这里其实也不是这么简单:
看下面的例子:Math.Round(3.44, 1); //Returns 3.4.Math.Round(3.45, 1); //Returns 3.4.Math.Round(3.46, 1); //Returns 3.5.
依照他的例子得到的是"五舍六入",我改变了一下数字得到的结果将完全改变。
Math.Round(3.445, 1); //Returns 3.4.
Math.Round(3.455, 1); //Returns 3.5.
Math.Round(3.465, 1); //Returns 3.5.
Math.Round(3.450, 1); //Returns 3.4.(补0是无效的)
Math.Round(3.4452, 2); //Returns 3.45.
Math.Round(3.4552, 2); //Returns 3.46.
Math.Round(3.4652, 2); //Returns 3.47.
-----------------------------------------------
Math.Round(3.44, 1) = 3.4
Math.Round(3.45, 1) = 3.4
Math.Round(3.46, 1) = 3.5
-----------------------------------------------
Math.Round(3.54, 1) = 3.5
Math.Round(3.55, 1) = 3.6
Math.Round(3.56, 1) = 3.6
-----------------------------------------------
Math.Round(3.64, 1) = 3.6
Math.Round(3.65, 1) = 3.6
Math.Round(3.66, 1) = 3.7
-----------------------------------------------
Math.Round(3.74, 1) = 3.7
Math.Round(3.75, 1) = 3.8
Math.Round(3.76, 1) = 3.8
这种舍入方法叫做银行家舍入(Banker'sRound),这就是已经规定下来的标准、Round的标准、世界的标准。
这里,其实是根据方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或银行家舍入;
总结的口诀是:四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一!
按照这样的口诀去做确实能验证以上的返回结果!
原来不是四舍五入这么简单,其实是四舍六入,但是五是很有讲究的。要是遇到五则取离前一位最近的偶数。比如,这条题目,11.5返回值就是12.0;-11.5返回值就是-12.0;
但是这里其实也不是这么简单:
看下面的例子:Math.Round(3.44, 1); //Returns 3.4.Math.Round(3.45, 1); //Returns 3.4.Math.Round(3.46, 1); //Returns 3.5.
依照他的例子得到的是"五舍六入",我改变了一下数字得到的结果将完全改变。
Math.Round(3.445, 1); //Returns 3.4.
Math.Round(3.455, 1); //Returns 3.5.
Math.Round(3.465, 1); //Returns 3.5.
Math.Round(3.450, 1); //Returns 3.4.(补0是无效的)
Math.Round(3.4452, 2); //Returns 3.45.
Math.Round(3.4552, 2); //Returns 3.46.
Math.Round(3.4652, 2); //Returns 3.47.
-----------------------------------------------
Math.Round(3.44, 1) = 3.4
Math.Round(3.45, 1) = 3.4
Math.Round(3.46, 1) = 3.5
-----------------------------------------------
Math.Round(3.54, 1) = 3.5
Math.Round(3.55, 1) = 3.6
Math.Round(3.56, 1) = 3.6
-----------------------------------------------
Math.Round(3.64, 1) = 3.6
Math.Round(3.65, 1) = 3.6
Math.Round(3.66, 1) = 3.7
-----------------------------------------------
Math.Round(3.74, 1) = 3.7
Math.Round(3.75, 1) = 3.8
Math.Round(3.76, 1) = 3.8
这种舍入方法叫做银行家舍入(Banker'sRound),这就是已经规定下来的标准、Round的标准、世界的标准。
这里,其实是根据方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或银行家舍入;
总结的口诀是:四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一!
按照这样的口诀去做确实能验证以上的返回结果!
相关文章推荐
- Java中Integer与int类型的比较--装箱和拆箱
- BloomFilter——大规模数据处理利器
- 黑马程序员_java基础之多线程
- Andrew Ng's Machine Learning in Coursera(I)
- socket、fsockopen、curl、stream 区别
- 内存溢出servlet
- Linux下的软件安装
- textview的一些设置 删除线 省略号 下划线 字体变化等
- 微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)
- HDU 1038.Biker's Trip Odometer【单位换算】【水水水】【8月24】
- php抓取页面的几种方式
- Cocos2d-x_创建多个滚动层(每层速度不同)
- nc推单---弹出新单据框
- vc++6.0 字节运算
- PHP的单引号和双引号
- hdu--5137
- Check the difficulty of problems POJ 2151
- POJ 3321 Apple Tree 树状数组
- const介绍
- 使用Go快速创建静态服务