toFixed (Jscript 5.5)才支持,
为了版本兼容,我们得自己写函数实现.<script>
// by wanghr100
/* 这样,就可以直接用toFixed()了.*/
Number.prototype.toFixed=function(len)
{
var add = 0;
var s,temp;
var s1 = this + "";
var start = s1.indexOf(".");
if(s1.substr(start+len+1,1)>=5)add=1;
var temp = Math.pow(10,len);
s = Math.floor(this * temp) + add;
return s/temp;
}
alert((52.277).toFixed(2))
alert((100.024).toFixed(1))
</script>
为了版本兼容,我们得自己写函数实现.<script>
// by wanghr100
/* 这样,就可以直接用toFixed()了.*/
Number.prototype.toFixed=function(len)
{
var add = 0;
var s,temp;
var s1 = this + "";
var start = s1.indexOf(".");
if(s1.substr(start+len+1,1)>=5)add=1;
var temp = Math.pow(10,len);
s = Math.floor(this * temp) + add;
return s/temp;
}
alert((52.277).toFixed(2))
alert((100.024).toFixed(1))
</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 += "00000000000000000000000000";
return s.substr(0,s.indexOf('.')+dot+1);
}
}
alert((52.277).toFixed(2))
alert((100.024).toFixed(1))