zoukankan      html  css  js  c++  java
  • 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;
            }

  • 相关阅读:
    硬盘安装windows2008的方法
    win7,win2008R2的vs2008 破解方法
    学习正则表达式
    C#里内置的DateTime基本功能
    jQuery Ajax 实例 全解析
    TreeView 部署后不能显示图标、js出错原因
    js 操作Listbox js 获取Listbox选择的值的代码
    ajax调用后台Datatable
    转:jquery刷新页面 页面跳转 js常用函数
    GridView多行表头的实现
  • 原文地址:https://www.cnblogs.com/kkwoo/p/3388129.html
Copyright © 2011-2022 走看看