js里 toFixed()方法的坑!

最近发现JS当中toFixed()方法存在一些问题,采用原生的Number对象的原型对象上的toFixed()方法时,规则并不是所谓的“四舍五入”或者是“四舍六入五成双”,所谓“四舍六入五成双”,在百度百科上给的解释是:也即“4舍6入5凑偶”这里“四”是指≤4 时舍去,”六”是指≥6时进上,”五”指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:①5前为奇数,舍5入1;②5前为偶数,舍5不进。(0是最小的偶数) 。百度百科上涉及的几个例子在实际情况下确实成立,但不科学,并不能覆盖所有的情况。

测试浏览器:屌丝浏览器IE6以及高级屌丝浏览器IE78和所有现代主流浏览器包括IE9、IE10、FF、chrome、opera、safari。(注:在使用IE10的类似firebug的开发工具时,采用兼容IE低版本浏览器模式时的测试结果跟使用原生低版本IE浏览器的测试结果不一致)

在浮点数末尾≤4或者≥6的情况下的舍入没有争议,但当末尾正好等于5的情况下可谓混乱之极 .....

文章来自:

http://www.toolscreator.com/?p=644


解决方案:重写这个方法

1 <html>
2 <head>
3 <script type=”text/javascript”>
4 Number.prototype.toFixed=function (d) { 
5 var s=this+“”; 
6 if(!d)d=0; 
7 if(s.indexOf(“.“)==-1)s+=“.“; 
8 s+=new Array(d+1).join(“0“); 
9 if(new RegExp(“^(-|\\+)?(\\d+(\\.\\d{0,“+(d+1)+“})?)\\d*$“).test(s)){
10 var s=“0“+RegExp.$2,pm=RegExp.$1,a=RegExp.$3.length,b=true;
11 if(a==d+2){
12 a=s.match(/\d/g); 
13 if(parseInt(a[a.length-1])>4){
14 for(var i=a.length-2;i>=0;i–){
15 a[i]=parseInt(a[i])+1;
16 if(a[i]==10){
17 a[i]=0;
18 b=i!=1;
19 }else break;
20 }
21 }
22 s=a.join(“”).replace(new RegExp(“(\\d+)(\\d{“+d+“})\\d$“),“$1.$2“);
23 
24 }if(b)s=s.substr(1); 
25 return (pm+s).replace(/\.$/,“”);
26 }return this+“”;
27 
28 };
29 </script>
30 </head>
31 <body>
32 <input type=”button” value=”显示0.009.toFixed(2)” οnclick=”alert(0.009.toFixed(2))”><br />
33 <input type=”button” value=”显示0.123.toFixed(2)” οnclick=”alert(0.123.toFixed(2))”><br />
34 <input type=”button” value=”显示0.125.toFixed(2)” οnclick=”alert(0.125.toFixed(2))”><br />
35 <input type=”button” value=”显示0.126.toFixed(2)” οnclick=”alert(0.126.toFixed(2))”><br />
36 <input type=”button” value=”显示20.445.toFixed(2)” οnclick=”alert(20.445.toFixed(2))”><br />
37 <input οnclick=”alert(20.405.toFixed(2))” type=”button” value=”显示20.405.toFixed(2)”> <br />
38 <input οnclick=”alert(20.415.toFixed(2))” type=”button” value=”显示20.415.toFixed(2)”> <br />
39 <input οnclick=”alert(20.425.toFixed(2))” type=”button” value=”显示20.425.toFixed(2)”> <br />
40 <input οnclick=”alert(20.435.toFixed(2))” type=”button” value=”显示20.435.toFixed(2)”> <br />
41 <input οnclick=”alert(20.445.toFixed(2))” type=”button” value=”显示20.445.toFixed(2)”> <br />
42 <input οnclick=”alert(20.455.toFixed(2))” type=”button” value=”显示20.455.toFixed(2)”> <br />
43 <input οnclick=”alert(20.465.toFixed(2))” type=”button” value=”显示20.465.toFixed(2)”> <br />
44 <input οnclick=”alert(20.475.toFixed(2))” type=”button” value=”显示20.475.toFixed(2)”> <br />
45 <input οnclick=”alert(20.485.toFixed(2))” type=”button” value=”显示20.485.toFixed(2)”> <br />
46 <input οnclick=”alert(20.495.toFixed(2))” type=”button” value=”显示20.495.toFixed(2)”> <br />
47 <input οnclick=”alert(0.05.toFixed(1))” type=”button” value=”显示0.05.toFixed(1)”> <br />
48 <input οnclick=”alert(0.15.toFixed(1))” type=”button” value=”显示0.15.toFixed(1)”> <br />
49 <input οnclick=”alert(0.25.toFixed(1))” type=”button” value=”显示0.25.toFixed(1)”> <br />
50 <input οnclick=”alert(0.35.toFixed(1))” type=”button” value=”显示0.35.toFixed(1)”> <br />
51 <input οnclick=”alert(0.45.toFixed(1))” type=”button” value=”显示0.45.toFixed(1)”> <br />
52 <input οnclick=”alert(0.55.toFixed(1))” type=”button” value=”显示0.55.toFixed(1)”> <br />
53 <input οnclick=”alert(0.65.toFixed(1))” type=”button” value=”显示0.65.toFixed(1)”> <br />
54 <input οnclick=”alert(0.75.toFixed(1))” type=”button” value=”显示0.75.toFixed(1)”> <br />
55 <input οnclick=”alert(0.85.toFixed(1))” type=”button” value=”显示0.85.toFixed(1)”> <br />
56 <input οnclick=”alert(0.95.toFixed(1))” type=”button” value=”显示0.95.toFixed(1)”> <br />
57 </body>
58 </html>


回答: 在JavaScript中,toFixed()方法用于将数字转换为指定小数位数的字符串表示。该方法接受一个参数,即要保留的小数位数。\[1\] 例如,如果我们有一个数字324.12,并使用toFixed(1),则结果将是324.1。同样,如果我们使用toFixed(3),结果将是324.120。\[1\] 但是,toFixed()方法在处理某些情况下可能会出现精度问题。为了解决这个问题,有人重写了toFixed()方法,通过扩大原数字的倍数来保留指定的小数位数,并使用Math.floor()方法来获取最接近的整数。\[2\] 但是,有时候在使用toFixed()方法时,可能会出现死循环的问题,这可能是因为在toFixed()方法中回调了toFixed()方法,导致无法退出循环。\[3\] 因此,在使用toFixed()方法时,需要注意这些问题,并根据具体情况选择合适的解决方案。 #### 引用[.reference_title] - *1* [js中toFixed()方法](https://blog.csdn.net/greek7777/article/details/120464309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [JS的toFixed方法](https://blog.csdn.net/qq_40805231/article/details/101271638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值