Math.round(),但只能取到整数位,如果要取到第一个小数位得这样写:
Math.round(numval*10)/10

解决方案 »

  1.   

    转贴: http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=127226
    在一個輸入框中,如何在onblur事件發生前(焦點離開前),不管輸入數字有多少位小數,但Value值為四舍五入後小數點保留兩位?  
    ---------------------------------------------------------------  
     
    <input  type="text"  name="textfield"  onblur="javascript:this.value=parseFloat(this.value).toFixed(2)"  onKeyUp="this.value=this.value.replace(/[^0-9.]/,'');">  
    ---------------------------------------------------------------  
     
    <input  type="text"  name="textfield"  onblur="javascript:this.value=Math.round(parseFloat(this.value)*100)/100"  onKeyUp="this.value=this.value.replace(/[^0-9.]/,'');">  
     
      

  2.   

    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((133.996).toFixed(2));