整数实现千分位 “,”
function formatNum(str) {
var newStr = "";
var count = 0;
if (str.indexOf(".") == -1) {
for (var i = str.length - 1; i >= 0; i--) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + "," + newStr;
} else {
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr;
return str;
}
}
var n = formatNum("1453153");
console.log(n);
小数实现千分位 “,”
function formatNum(val) {
var newStr = "";
var count = 0;
var str = val.split(".")[0];
if (str.indexOf(".") == -1) {
for (var i = str.length - 1; i >= 0; i--) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + "," + newStr;
} else {
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + "." + val.split(".")[1];
return str;
}
}
var n = formatNum("1453105453.1231");
console.log(n);
Done!