document.write("1.255:"+(1.255).toFixed(2)+"<br/>");
//重写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;
};
document.write("2.255:"+(2.255).toFixed(2));
IE6下为:
1.26
2.26
Chrome下为:
1.25
2.25
因此引用了哥覆盖方法,做到兼容
//重写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;
};