Created
April 24, 2014 04:53
-
-
Save hengkiardo/11241965 to your computer and use it in GitHub Desktop.
How to format a number to string
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 nFormatter(num) { | |
if (num >= 1000000000) { | |
return (num / 1000000000).toFixed(2).replace(/\.0$/, '') + 'G'; | |
} | |
if (num >= 1000000) { | |
return (num / 1000000).toFixed(2).replace(/\.0$/, '') + 'M'; | |
} | |
if (num >= 1000) { | |
return (num / 1000).toFixed(2).replace(/\.0$/, '') + 'K'; | |
} | |
return num; | |
} | |
console.log(nFormatter(7722653)) | |
// outout : "7.72M" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment