function _comma(number) {
// 补全代码
//需要加的逗号个数
let res = Math.floor(number.toString().length / 3)
if (res <= 1) {
return number.toString()
} else {
const arr = [...number.toString()].reverse()
//长度正好为3的倍数则第一个数之前不需要加,
if (number.toString().length % 3 == 0) {
for (let index = 1; index < res; index++) {
arr.splice(index * 3 + index - 1, 0, ',')
}
}else {
for (let index = 1; index <= res; index++) {
arr.splice(index * 3 + index - 1, 0, ',')
}
}
return arr.reverse().join('')
}
}
//功能测试
const num = 1234564;
const newNum = _comma(num)
console.log(newNum)