Anyone who has used the toFix() method in js should know that there is a small BUG in this method.
The carry of decimals is a little different under IE and FF.
For example (0.005) toFix(2)=0.00 under ie. toFix(2)=0.01 under FF.
This will cause data differences.
We can achieve unification of precision by overriding this method.
Copy the code code as follows:
Number.prototype.toFixed = function(s)
{
return (parseInt(this * Math.pow( 10, s ) + 0.5)/ Math.pow( 10, s )).toString();
}
But there is still a problem with this. In all browsers, String("0.050").toFix(2)=0.1
We can see that you originally wanted to keep two decimal places but ended up with one decimal place. That is to say. This override only toFixed() will automatically discard the last 0.
We need to do further processing on this method.
Copy the code code as follows:
Number.prototype.toFixed = function(s)
{
changenum=(parseInt(this * Math.pow( 10, s ) + 0.5)/ Math.pow( 10, s )).toString();
index=changenum.indexOf(".");
if(index<0&&s>0){
changenum=changenum+".";
for(i=0;i<s;i++){
changenum=changenum+"0";
}
}else {
index=changenum.length-index;
for(i=0;i<(s-index)+1;i++){
changenum=changenum+"0";
}
}
return changenum;
}