js数值千分隔(正则)

js数值千分隔

number_format (number, decimals, decPoint, thousandsSep) {
        /*
        * 参数说明:
        * number:要格式化的数字
        * decimals:保留几位小数
        * decPoint:小数点符号
        * thousandsSep:千分位符号
        * */
        number = (number + '').replace(/[^0-9+-Ee.]/g, '');
        let n = !isFinite(+number) ? 0 : +number;
        let prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
        let sep = thousandsSep;
        let dec = decPoint;
        let s = '';
        let toFixedFix = function (n, prec) {
          let k = Math.pow(10, prec);
          return '' + Math.ceil(n * k) / k;
        };
        s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
        let re = /(-?\d+)(\d{3})/;
        while (re.test(s[0])) {
            s[0] = s[0].replace(re, '$1' + '' + sep + '$2');
        }
        if ((s[1] || '').length < prec) {
            s[1] = s[1] || '';
            s[1] += new Array(prec - s[1].length + 1).join('0');
        }
        return s.join(dec);
    }

number_format(1111.12345, 2, ".", ",")

end.

posted @ 2019-12-30 14:19  wxid_m2pywu7fxu1f  阅读(356)  评论(0编辑  收藏  举报