- varmoney=0.00542;//0.006;
- alert(Number(money).toFixed(2));
- //0.00
var money=0.00542;//0.006;
alert(Number(money).toFixed(2));
//0.00
可以看出上面的结果是错误的,下面的方法通过巧妙的使用Math.round函数,完全可以解决数值很小时的精度问题。
- varmoney=0.00542;//0.006;
- alert(Number(money).toFixed(2));
- functionround2(number,fractionDigits){
- with(Math){
- returnround(number*pow(10,fractionDigits))/pow(10,fractionDigits);
- }
- }
- alert(round2(money,2));//0.01
本文介绍了一种解决JavaScript中处理小数值时出现的精度问题的方法。通过使用Math.round函数结合乘除操作,可以有效避免toFixed方法在处理小于0.01的小数时显示为0.00的问题。
1170

被折叠的 条评论
为什么被折叠?



