3.135.toFixed(2);  结果是3.13 我要的是3.14
Math.round(2.135*100)/100;  结果是2.13 我要的结果是2.14

解决方案 »

  1.   


    function round(num,point){
      if(isNaN(num)){
        return null;
      }
      point=Math.pow(10,point);
      num=num*point;
      if(num===+num){
        return parseInt(num+0.5)/point;
      }else{
        return +num;
      }
    }
    var a=2147483647.5657;
    console.log("result:"+round(a,2));
      

  2.   

    修正:function round(num,point){
      if(isNaN(num)){
        return null;
      }
      point=Math.pow(10,point+1);
      num=num*point;
      if(num===+num){
        return parseInt((num+5)/10)/point*10;
      }else{
        return +num;
      }
    }
    var a=2.135;
    console.log("resu:"+round(a,2));
      

  3.   

    Math.ceil(3.135*100)/100
      

  4.   

    2.005变成2.0100000000000002 了
    把“ return parseInt((num+5)/10)/point*10;  ”
    改成“  return parseInt((num+5)/10)/(point/10); ”
      

  5.   

    Math.ceil(2.3401*100)/100
    结果为 2.35  但我要的是四舍五入为2.34
      

  6.   

    自己写一个js函数,将float类型数据装换成string的类型然后截取你省略的那位的后一位和5比较。
    将结果加上你使用toFixed的值就好了
      

  7.   

    四舍五入保留任意位的小数,如果有错请指正function roundFixed(num, fixed) {
    var pos = num.toString().indexOf('.'),
    decimal_places = num.toString().length - pos - 1,
    _int = num * Math.pow(10, decimal_places),
    divisor_1 = Math.pow(10, decimal_places - fixed),
    divisor_2 = Math.pow(10, fixed);
    return Math.round(_int / divisor_1) / divisor_2;
    }console.log(roundFixed(2147483647.5657,2));//-> 2147483647.57
      

  8.   

    var a = 3.135;
    alert((Math.round(a*100)/100).toFixed(2))
      

  9.   


    function formatFloat(src, pos){
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
    }
    var num = 3.145;
    alert(formatFloat(num,2))
      

  10.   

    Math.ceil(2.3401*100)/100
    结果为 2.35  但我要的是四舍五入为2.34四舍五入用 Math.round
    Math.floor()  向下取整
    Math.ceil() 向上取整(进1)
      

  11.   

    formatFloat = num => isNaN(num) ? new Error('数字格式'):(Math.round(num * 100) / 100).toFixed(2)formatFloat (3.1901) //3.20formatFloat ('') //err