JS实现千位分隔符
// given 123456
let input = 123456
// output 123,456const f = (str) => {
return str.replace(/(?=(\B\d{3}+$))/g/, ',')
}const f = (num) => {
if(typeof num !== 'number') throw Error('not number input')
const str = `${num}`
let count = 0, ret = []
for(let i=str.length-1; i>=0 ; i--) {
count++
ret.unshift(str[i])
if(count === 3 && i !=0) {
ret.unshift(',')
count = 0
}
}
return ret.join('')
}最后更新于