Number#toFixed()
对于某些数字无法正常工作。
例如:-
7.795.toFixed(2)
//-> 7.79 #Instead it should display 7.80
8.895.toFixed(2)
//-> 8.89 #Instead it should display 8.90
1.105.toFixed(2)
//-> 1.10 #Instead it should display 1.11
55.305.toFixed(2)
//-> 55.30 #Instead it should display 55.31
请为我提供解决此问题的方法。
最佳答案
function round_float(x,n){
if(!parseInt(n))
var n=0;
if(!parseFloat(x))
return false;
return Math.round(x*Math.pow(10,n))/Math.pow(10,n);
}
round_float(1.105,2).toFixed(2);
//结果:1.11
关于javascript - `toFixed()` 对某些数字无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912398/