<SCRIPT LANGUAGE="JavaScript">
try {
(1).toFixed(1);
}
catch(e) {
Number.prototype.toFixed = function(dot) {
with(Math){
var m=pow(10,Number(dot))
var s = (round(this*m)/m).toString();
}
if(s.indexOf('.') < 0)
s += ".";
s += "000000000000";
return s.substr(0,s.indexOf('.')+dot+1);
}
}n = 0.22999999;
alert(n.toFixed(2));
</script>
try {
(1).toFixed(1);
}
catch(e) {
Number.prototype.toFixed = function(dot) {
with(Math){
var m=pow(10,Number(dot))
var s = (round(this*m)/m).toString();
}
if(s.indexOf('.') < 0)
s += ".";
s += "000000000000";
return s.substr(0,s.indexOf('.')+dot+1);
}
}n = 0.22999999;
alert(n.toFixed(2));
</script>
function tofloat(f,dec) {
if(dec<0) return "Error:dec<0!";
result=parseInt(f)+(dec==0?"":".");
f-=parseInt(f);
if(f==0)
for(i=0;i<dec;i++) result+='0';
else {
for(i=0;i<dec;i++) f*=10;
result+=parseInt(Math.round(f));
}
return result;
}
alert(tofloat(11.20000000000000000001,2))
</script>
Math.round(parseFloat(0.22999999)*1000)/1000 这样就保留三位小数了,以此类推
Math.round(parseFloat(0.22999999)*100)/100 这样是保留两位小数
Math.round(parseFloat(0.22999999)*1000)/1000 这样就保留三位小数了,以此类推
Number.prototype.fix = function(num)
{with(Math)return round(this.valueOf()*pow(10,num))/pow(10,num);
}
alert((5.31-1.31).fix(2));
</script>