• Js千位分隔符


    //JS实现千位分隔符
            function formatNumber(nbr) {
                if (nbr && nbr > 1000) {
                    //1. 从后往前插进数组,然后 join(",");
                    var newNbr = nbr.toString();
                    var arr = [];
                    while (newNbr > 1000) {
                        var idx = newNbr.indexOf(".");
                        if (idx > -1) { //包含小数
                            arr.push(newNbr.substr(idx - 3, newNbr.length - (idx - 3)));
                            newNbr = newNbr.substr(0, idx - 3);
                        }
                        else {
                            arr.push(newNbr.substr(newNbr.length - 3, 3));
                            newNbr = newNbr.substr(0, newNbr.length - 3);
                        }
                    }
                    arr.push(newNbr);
                    arr.reverse();
                    return arr.join(",");
                }
                else {
                    return nbr;
                }
            }
           
            //JS使用正则实现千位分割符
            function commafy(num) {
                num = num + "";
                var re = /(-?d+)(d{3})/;
                while (re.test(num)) {
                    num = num.replace(re, "$1,$2");
                }
                return num;
            }

  • 相关阅读:
    Ducking
    MINITAB(二)
    JFreechart
    linux命令0424
    JAVA哈哈镜
    HTML(四)
    The 3n+1 problem
    [转载]:【读书笔记】.NET本质论
    ER图基本步骤
    [从架构到设计]第一回:设计,应该多一点(转载)
  • 原文地址:https://www.cnblogs.com/kkwoo/p/3388129.html
Copyright © 2020-2023  润新知