js 控制页面显示有限小数

  // 保留三位小数,不强制
    function changeTwoDecimal(x){
        var f_x = parseFloat(x);
        if (isNaN(f_x)){
            jFail("message.please.enter.right.number");
            return false;
        }
        f_x = Math.round(f_x *1000)/1000;
        return f_x;
    }
    // 强制保留三位 3==>3.000
    function changeThreeDecimal_f(x) {
        var f_x = parseFloat(x);
        if (isNaN(f_x)){
            return false;
        }
        f_x = Math.round(f_x*100)/100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0){
            pos_decimal = s_x.length;
            s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2) {
            s_x += '0';
        }
        return s_x;
    }

 

posted on 2017-11-15 09:21  钉子His  阅读(229)  评论(0编辑  收藏  举报

导航