JS中toFixed()方法的问题及解决方案
最近发现JS当中toFixed()方法存在一些问题,采用原生的Number对象的原型对象上的toFixed()方法时,规则并不是所谓的“四舍五入”或者是“四舍六入五成双”,所谓“四舍六入五成双”,在百度百科上给的解释是:也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是最小的偶数) 。百度百科上涉及的几个例子在实际情况下确实成立,但不科学,并不能覆盖所有的情况。
测试浏览器:屌丝浏览器IE6以及高级屌丝浏览器IE78(此处为方便未使用原生IE678,不过IETester破天荒地表现良好,精确做法应该是一个版本对应一个虚拟机来测试)和所有现代主流浏览器包括IE9、IE10、FF、Chrome、Opera、Safari。(注:在使用IE10的类似firebug的开发工具时,采用兼容IE低版本浏览器模式时的测试结果跟使用原生低版本IE浏览器的测试结果不一致)
在浮点数末尾≤4或者≥6的情况下的舍入没有争议,但当末尾正好等于5的情况下可谓混乱之极。
一、FF 稳定版测试结果如下所示:
二、FF Aurora测试结果如下所示:
三、FF Nightly的测试结果如下所示:
四、Chrome稳定版的测试结果如下所示:
五、Chromium的测试结果如下所示:
六、Chrome Canary的测试结果如下所示:
七、Opera稳定版的测试结果如下所示:
八、Opera Next的测试结果如下所示:
九、Safari的测试结果如下所示:
十、IE6-8的测试结果如下所示:
十一、IE9、10的测试结果如下所示:
总结:众所周知,遵循IEEE754数值格式的语言的浮点计算会出现精度损耗的通病,ES也并非独此一家,因此尽量不要进行某个特定浮点数值的测试,如:0.1+0.2;
解决方案:重写Number.prototype.toFixed()方法:
<html>
<head>
<script type="text/javascript">
Number.prototype.toFixed=function (d) {
var s=this+"";
if(!d)d=;
if(s.indexOf(".")==-)s+=".";
s+=new Array(d+).join("");
if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+)+"})?)\\d*$").test(s)){
var s=""+RegExp.$,pm=RegExp.$,a=RegExp.$.length,b=true;
if(a==d+){
a=s.match(/\d/g);
if(parseInt(a[a.length-])>){
for(var i=a.length-;i>=;i--){
a[i]=parseInt(a[i])+;
if(a[i]==){
a[i]=;
b=i!=;
}else break;
}
}
s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2"); }if(b)s=s.substr();
return (pm+s).replace(/\.$/,"");
}return this+""; };
</script>
</head>
<body>
<input type="button" value="显示0.009.toFixed(2)" onclick="alert(0.009.toFixed(2))"><br />
<input type="button" value="显示0.123.toFixed(2)" onclick="alert(0.123.toFixed(2))"><br />
<input type="button" value="显示0.125.toFixed(2)" onclick="alert(0.125.toFixed(2))"><br />
<input type="button" value="显示0.126.toFixed(2)" onclick="alert(0.126.toFixed(2))"><br />
<input type="button" value="显示20.445.toFixed(2)" onclick="alert(20.445.toFixed(2))"><br />
<input onclick="alert(20.405.toFixed(2))" type="button" value="显示20.405.toFixed(2)"> <br />
<input onclick="alert(20.415.toFixed(2))" type="button" value="显示20.415.toFixed(2)"> <br />
<input onclick="alert(20.425.toFixed(2))" type="button" value="显示20.425.toFixed(2)"> <br />
<input onclick="alert(20.435.toFixed(2))" type="button" value="显示20.435.toFixed(2)"> <br />
<input onclick="alert(20.445.toFixed(2))" type="button" value="显示20.445.toFixed(2)"> <br />
<input onclick="alert(20.455.toFixed(2))" type="button" value="显示20.455.toFixed(2)"> <br />
<input onclick="alert(20.465.toFixed(2))" type="button" value="显示20.465.toFixed(2)"> <br />
<input onclick="alert(20.475.toFixed(2))" type="button" value="显示20.475.toFixed(2)"> <br />
<input onclick="alert(20.485.toFixed(2))" type="button" value="显示20.485.toFixed(2)"> <br />
<input onclick="alert(20.495.toFixed(2))" type="button" value="显示20.495.toFixed(2)"> <br />
<input onclick="alert(0.05.toFixed(1))" type="button" value="显示0.05.toFixed(1)"> <br />
<input onclick="alert(0.15.toFixed(1))" type="button" value="显示0.15.toFixed(1)"> <br />
<input onclick="alert(0.25.toFixed(1))" type="button" value="显示0.25.toFixed(1)"> <br />
<input onclick="alert(0.35.toFixed(1))" type="button" value="显示0.35.toFixed(1)"> <br />
<input onclick="alert(0.45.toFixed(1))" type="button" value="显示0.45.toFixed(1)"> <br />
<input onclick="alert(0.55.toFixed(1))" type="button" value="显示0.55.toFixed(1)"> <br />
<input onclick="alert(0.65.toFixed(1))" type="button" value="显示0.65.toFixed(1)"> <br />
<input onclick="alert(0.75.toFixed(1))" type="button" value="显示0.75.toFixed(1)"> <br />
<input onclick="alert(0.85.toFixed(1))" type="button" value="显示0.85.toFixed(1)"> <br />
<input onclick="alert(0.95.toFixed(1))" type="button" value="显示0.95.toFixed(1)"> <br />
</body>
</html>
亲测有效,原文链接
JS中toFixed()方法的问题及解决方案的更多相关文章
- Js中toFixed()方法保留小数不精准的问题
toFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 问题:部分特殊数值使用toFixed() 方法会出现转换不正确的情况,举个例子: (3329.225).toFixed(2) ...
- 【转载】JS中bind方法与函数柯里化
原生bind方法 不同于jQuery中的bind方法只是简单的绑定事件函数,原生js中bind()方法略复杂,该方法上在ES5中被引入,大概就是IE9+等现代浏览器都支持了(有关ES5各项特性的支持情 ...
- 原生JS中apply()方法的一个值得注意的用法
今天在学习vue.js的render时,遇到需要重复构造多个同类型对象的问题,在这里发现原生JS中apply()方法的一个特殊的用法: var ary = Array.apply(null, { &q ...
- js中apply方法的使用
js中apply方法的使用 1.对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是: Object.extend = function(destinat ...
- paip.编程语言方法重载实现的原理及python,php,js中实现方法重载
paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...
- js中settimeout方法加参数
js中settimeout方法加参数的使用. 简单使用看w3school 里面没有参数调用, 例子: <script type="text/javascript"> ...
- js中split()方法得到的数组长度
js 中split(",")方法通过 ”,“ 分割字符串, 如果字符串中没有 “,” , 返回的是字符串本身 var str = “abc”://分隔符个数为0 var newSt ...
- 关于JS中的方法是否加括号的问题
js中的方法什么时候加括号什么时候不加括号呢,我们有时候经常就搞不清楚,记住下面这几点就好理解了. 1.函数做参数时都不要加括号. function fun(a){ alert(a); } funct ...
- js中的方法如何传入多个参数
js中的方法如何传入多个参数 $(function () { let parameter1 = 1; let parameter2 = 'Hello World'; let parameter3 = ...
随机推荐
- Jquery.Datatables td宽度太长的情况下,自动换行
在 td 里面 加上 style="word-wrap:break-word;" 自动换行就好了,如果不想换行,可以将超出内容设为隐藏, overflow:hidden; whit ...
- C#的yield关键字
using System; using System.Collections.Generic; using System.Reflection; using System.Text.RegularEx ...
- 使用Memcached Session Manager扩展Session管理
>>Tomcat的session管理 在请求过程中首先要解析请求中的sessionId信息,然后将sessionId存储到request的参数列表中. 然后再从request获取sessi ...
- js自定义延迟执行函数
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ASP.NET Web Api 服务器端变了,客户端该如何修改请求(转载)
转载地址:http://www.cnblogs.com/fzrain/p/3558765.html 前言 一旦我们将API发布之后,消费者就会开始使用并和其他的一些数据混在一起.然而,当新的需求出现时 ...
- DIV宽度自动缓慢变化
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Install Docker on Mac OS X(转)
Install Docker on Mac OS X You can install Docker using Boot2Docker to run docker commands at your c ...
- html5 svg
html5 svg <html > <body> <p>canvas 用js 绘画,是整幅画布,适合游戏 svg可放大,支持dom 操作,js事件 线性渐变.高斯模 ...
- 几个国内速度最快的centos yum(更新源)
转自:http://blog.itpub.net/15711267/viewspace-1068862/ 中国科技大学源 cd /etc/yum.repos.d mv CentOS-Base.repo ...
- MongoDB学习(2)—Node.js与MongoDB的基本连接示例
前提 已经安装了node.js和MongoDB,本文使用的node.js是v0.12.0,MongoDB是3.0.0. 初始化数据 启动MongoDB服务,在test数据库中插入一条实例数据: db. ...