Skip to content

Instantly share code, notes, and snippets.

@dominhhai
Last active May 27, 2016 08:10
Show Gist options
  • Save dominhhai/b9e8fa314a5a385db241c4db5f53b788 to your computer and use it in GitHub Desktop.
Save dominhhai/b9e8fa314a5a385db241c4db5f53b788 to your computer and use it in GitHub Desktop.
Format integer number
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