日期:2014-05-16  浏览次数:20427 次

JS实现整数的千分位分割形式
function FormatNumber(intInput) {
            //将输入参数转换为字符串形式
            var strInput = Math.abs(intInput).toString();
            //如果有小数,把小数部分提取出来
            var strXS = "";
            if (strInput.indexOf(".", 0) != -1) {
                strXS = strInput.substring(strInput.split(".")[0].length, strInput.length);
                strInput = strInput.split(".")[0];
            }
            //获取输入参数的长度
            var iLen = strInput.length;
            //如果输入参数的长度小于等于3,则直接返回
            //否则,再进行处理
            if (iLen <= 3) {
                return intInput;
            } else {
                //首先取模,以作为起始点,每3位截取一次存入数组,最后再进行拼接返回
                var iMod = iLen % 3;
                //每3位截取的起始点 
                var iStart = iMod;
                //每3位截取的存储数组
                var aryReturn = [];

                //循环处理:每3位截取一次 存储到数组
                while (iStart + 3 <= iLen) {
                    aryReturn[aryReturn.length] = strInput.substring(iStart, iStart + 3);
                    iStart = iStart + 3;
                }
                //将数组中的数据连接起来
                aryReturn = aryReturn.join(",");

                //处理输入参数长度不是3的倍数的情况
                if (iMod != 0) {
                    aryReturn = strInput.substring(0, iMod) + "," + aryReturn;
                }
                //处理负数的情况
                if (intInput < 0) { aryReturn = "-" + aryReturn; }

   &nb