欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript之對系統(tǒng)的toFixed()方法的修正

 更新時間:2007年05月08日 00:00:00   作者:  
0.009.toFixed(2)本該返回0.01的結(jié)果,可它卻返回了一個0.00,這是這個方法的一個BUG,且這個方法對客戶端的JS版本要求有點(diǎn)偏高,至少在IE5.0里這個方法用不起來,所以我寫了上面的一段修正代碼,并且還解決了這個BUG的問題。若是想完全使用這個自定義的方法替代那個有BUG的系統(tǒng)方法的話,只需要去掉最外層的那個 if 判斷就可以了。 

//by meizz

if(typeof(Number.prototype.toFixed)!="function")
{

  Number.prototype.toFixed=function (d)
  {

    var s=this+"";
    if(!d)d=0;    
    if(s.indexOf(".")==-1)s+=".";
    s+=new Array(d+1).join("0");    
    if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+1)+"})?)\\d*$").test(s))
    {

      var s="0"+RegExp.$2,pm=RegExp.$1,a=RegExp.$3.length,b=true;      
      if(a==d+2){
        a=s.match(/\d/g);
        if(parseInt(a[a.length-1])>4)
        {

          for(var i=a.length-2;i>=0;i--){
            a[i]=parseInt(a[i])+1;            
            if(a[i]==10){
              a[i]=0;
              b=i!=1;

            }else break;

          }

        }
        s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");        

      }if(b)s=s.substr(1);
      return (pm+s).replace(/\.$/,"");

    }return this+"";    

  };  


相關(guān)文章

最新評論