Last active
May 27, 2016 08:10
-
-
Save dominhhai/b9e8fa314a5a385db241c4db5f53b788 to your computer and use it in GitHub Desktop.
Format integer number
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 format (n) { | |
var str = n.toString() | |
for (var i = str.length - 3; i > 0; i -= 3) { | |
str = str.substring(0, i) + ',' + str.substring(i) | |
} | |
return str | |
} | |
var arr = [10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000] | |
arr.forEach(function (n) { | |
console.log(format(n)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment