Skip to content

Instantly share code, notes, and snippets.

@farhadhp
Created April 28, 2021 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farhadhp/72ba79c9b2410d25fc3b1f19b0dd2b8f to your computer and use it in GitHub Desktop.
Save farhadhp/72ba79c9b2410d25fc3b1f19b0dd2b8f to your computer and use it in GitHub Desktop.
Javascript number format
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
console.log(formatNumber(1234)) // 1,234
console.log(formatNumber(123456)) // 123,456
console.log(formatNumber(123456789)) // 123,456,789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment