javascript - How to implement the function of inserting commas in numbers in JS?
世界只因有你2017-05-19 10:35:39
0
4
557
The requirement is from right to left, add a comma for every 3 numbers, add a comma for every 2 numbers, and so on. For example, 123456789 will become 1,234,56,789
function change(num,bool){
function tho(num){return num>1000?String(num).slice(-5,-3)+','+String(num).slice(-3):num}
return num<100000?tho(num):change(parseInt(num/100000))+","+tho(num)
}
Using regular expressions to complete the function in one line.