//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;
}