// 无小数版本
let format = n => {
let num = n.toString()
let len = num.length
if (len <= 3) {
return num
} else {
return format(num.slice(0,len-3))+','+num.slice(len-3)
}
}
let res=format(1232327666663) // '1,232,323'
console.log(res)
// 有小数版本
let format = n => {
let num = n.toString() // 转成字符串
let decimals = ''
// 判断是否有小数
let index=num.indexOf('.')
if(index>-1)
{
decimals = num.slice(index)
num=num.slice(0,index)
}
let len = num.length
let res=''
if (len <= 3) {
res = num
} else {
res = format(num.slice(0,len-3))+','+num.slice(len-3)
}
return decimals?res+decimals:res
}
let res=format(123232.7666663) // '1,232,323'
console.log(res)