Javascript实现千位分隔格式化数据

1.取余实现

 1     function thousands(num) {
 2       var result = []
 3       while (Math.floor(num / 1000)) {
 4         var temp = num % 1000
 5         result.push(temp)
 6         num = Math.floor(num / 1000)
 7       }
 8       result.push(num)
 9       return result.reverse().join(',')
10     }
11     console.log(thousands(11235784))

2. 正则实现

1 thousandBits(num) {
2       return (num + '').replace(/\d{1,3}(?=(\d{3})+$)/g, '$&,')
3 }

 

posted on 2019-02-13 18:53  时光游弋  阅读(313)  评论(0编辑  收藏  举报