Number.toFixed(fracDigits) fracDigits为小数点后的位数,只在IE5.5以上可行
IE5.5以下我用的是s=Number.toString(); s=s.substr(0,s.indexOf('.')+fracDigits);

解决方案 »

  1.   

    用VBScript来处理最方便.<Script language=VBScript>
    x=a/b;
    MsgBox(FormatNumber(x,2))
    </Script>
      

  2.   

    <script>
    String.prototype.fn = function(n)
      { s=""
        for(i=0;i<n;i++)s+=this
        return s
      }
    Number.prototype.fix = function(num)
      {with(Math)return (round(this.valueOf()*pow(10,num))/pow(10,num)).toString().search(/\./i)==-1?(round(this.valueOf()*pow(10,num))/pow(10,num)).toString()+"."+"0".fn(num):(round(this.valueOf()*pow(10,num))/pow(10,num));
      }
    alert((5.31-1.31).fix(2));
    </script>
    上边的代码返回任意数的保留n位小数值,为了考虑货币的效果(xx.xx)形势,所以长了一点儿,因为4.00的格式是不能显示的(以数字方式),如果不用顾及那个用下边的就行
    <script>
    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>
      

  3.   

    <script>
    function Number.prototype.Fixed(num){
    with(Math){
    var m=pow(10,Number(num))
    return round(this*m)/m;
    }
    }
    alert((2/3).Fixed(2))
    alert((2/3).toFixed(2));//ie5.5+
    </script>
      

  4.   

    对于IE6.0以上,直接用 your_number.toFixed(n)
      

  5.   

    回复人: zhongjz(斑马) ( ) 信誉:100  2003-04-24 00:23:00  得分:0 
       对于IE6.0以上,直接用 your_number.toFixed(n)
     
    是IE5.5+以上……
     
      

  6.   

    通用的toFixed
    <script>
    try {
      Number.toFixed();
    }
    catch(e) {
      function Number.prototype.toFixed(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);
      }
    }alert((2/3).toFixed(2));</script>