Math.round(n)
Math.floor(n)
Math.ceil(n)

解决方案 »

  1.   

    <script>
    var f = 10.5 ;
    alert(Math.round(f)) ;
    </script>
      

  2.   

    round 方法
    返回与给出的数值表达式最接近的整数。Math.round(number) 必选项 number 参数是要舍入到最接近整数的值。说明
    如果 number 的小数部分大于等于 0.5,返回值是大于 number 的最小整数。否则,round 返回小于等于 number 的最大整数。 floor 方法
    返回小于等于其数值参数的最大整数。 Math.floor(number)必选项 number 参数是数值表达式。说明
    返回值为小于等于其数值参数的最大整数值。 ceil 方法
    返回大于等于其数字参数的最小整数。 Math.ceil(number)必选项number 参数是数值表达式。说明
    返回值为大于等于其数字参数的最小整数。
      

  3.   

    <script>
    function round1(num){document.write(num+"(use round)=>"+Math.round(num)+"<br>")}
    function round2(num){document.write(num+"(use floor)=>"+Math.floor(num+0.5)+"<br>")}
    function round3(num){document.write(num+"(use ceil)=>"+Math.ceil(num-0.4)+"<br>")}
    round1(10.4);
    round1(10.5);
    round2(10.4);
    round2(10.5);
    round3(10.4);
    round3(10.5);
    </script>
      

  4.   

    1\
    ParseFloat(str).toFixed(2);// 2表示位数2\
    <script language=JavaScript>
    function tofloat(f,dec) { 
    if(dec<0) return "Error:dec<0!"; 
    result=parseInt(f)+(dec==0?"":"."); 
    f-=parseInt(f); 
    if(f==0) 
    for(i=0;i<dec;i++) result+='0'; 
    else { 
    for(i=0;i<dec;i++) f*=10; 
    result+=parseInt(Math.round(f)); 

    return result; 

    alert(tofloat(12.7879888,2))
    </script>
    3\
    <script>
    alert(parseInt(12.7879888*100+0.5)/100);
    </script>