将数字每千分位用逗号隔开
小于 1 分钟
将数字每千分位用逗号隔开
function format(n){
let num = n.toString()
let decimals = ''
let int = ''
if(num.includes('.')) {
[int, decimals] = num.split('.')
}
const end = decimals ? '.'+decimals : ''
if(int.length < 3){
return n
}else{
let left = int.length % 3
if(left > 0){
return int.slice(0, left) + ',' + int.slice(left).match(/\d{3}/g).join(',') + end
}else{
return int.slice(left).match(/\d{3}/g).join(',') + end
}
}
}
format(12345.88)