js转化 保留2位小数

时间:2014-08-13 18:21:36   收藏:0   阅读:233

    //转换小数点格式为float 保留2位xiaoshu     function changeTwoDecimal_f(x) {

        var f_x = parseFloat(x);

        if (isNaN(f_x)) {

            alert(‘function:changeTwoDecimal->parameter error‘);

            return false;

        }

        var f_x = Math.round(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;

    }    

js转化 保留2位小数,布布扣,bubuko.com

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!