Math.round()什么作用?
来源:互联网 发布:淘宝哪家大码女装好看 编辑:程序博客网 时间:2024/06/08 04:35
题目大概是问Math.Round(11.5);和Math.Round(-11.5);返回值是多少?
原来不是四舍五入这么简单,其实是四舍六入,但是五是很有讲究的。要是遇到五则取离前一位最近的偶数。比如,这条题目,11.5返回值就是12.0;-11.5返回值就是-11.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返回值就是-11.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 节。这种舍入有时称为就近舍入或银行家舍入;
总结的口诀是:四舍六入五考虑,五后非零就进一,五后皆零看奇偶,五前为偶应舍去,五前为奇要进一!
按照这样的口诀去做确实能验证以上的返回结果!
0 0
- Math.round()什么作用?
- Math.round()什么作用?
- Math.round()什么作用?
- Math.round()
- Math.Round
- Math.round
- Math.round()
- Math.Round
- Math.Round()
- Math.round()
- math。round and math。floor
- Math.ceil()和Math.floor()、Math.round()
- Math.round|Math.floor|Math.ceil区别
- Math.floor,Math.round,Math.ceil
- Math.round, Math.ceil 和 Math.floor
- Math.floor()/Math.ceil()/Math.round()
- Math.ceil、Math.floor、Math.round用法
- Math.Round函数说明
- Spring缓存注解@Cache使用
- 如何为你的 Node.js 应用挑选数据库
- 整理的一些iOS 开源框架
- MySQL数据库引擎详解
- ajax防止多次点击重复提交
- Math.round()什么作用?
- 关于Java的Array和Arrays
- 图存储1 临接矩阵
- 北邮人论坛搜索
- java面试之2
- 顶尖程序员与普通程序员的区别
- 工作流框架--activiti(一)--建库
- Android侦听应用(Package)变化的方法(2)实现PackageMonitor
- 原装Win8本改装Win7不完全指南