這個方法是在一個例子中看到的,我測試了一下是小數點后四舍五入的功能
例如,5.05---->toFixed(1) 5.1
5.056-------->toFixed(2) 5.06
但是用到0.056時就出現問題了toFixed(1)的結果是0.0
有點奇怪的答案
下面的腳本是重寫了toFixed(),這樣0.056就可以轉化到0.1了
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;
}
posted on 2008-06-23 16:18
湘江夜游神 閱讀(6488)
評論(4) 編輯 收藏 所屬分類:
JavaScript