Created
September 21, 2012 06:45
-
-
Save dexbol/3760062 to your computer and use it in GitHub Desktop.
add commas to integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addComma(num) { | |
num = String(num); | |
var len = num.length; | |
var remainder = len % 3; | |
var h = num.substring(0, remainder); | |
var str = num.substring(remainder); | |
var i = 0; | |
var flag = str.length / 3; | |
var result = h ? [h] : []; | |
for (; i <f lag; i ++) { | |
result.push(str.substring(i * 3, i * 3 + 3)); | |
} | |
return result.join(); | |
} | |
function addComma2 (num) { | |
num = String(num); | |
var reg = /(\d+)(\d{3})/; | |
while (reg.test(num)) { | |
num = num.replace(reg, '$1' + ',' + '$2'); | |
} | |
return num; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment