Created
April 28, 2021 22:19
-
-
Save farhadhp/72ba79c9b2410d25fc3b1f19b0dd2b8f to your computer and use it in GitHub Desktop.
Javascript number format
This file contains hidden or 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 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