Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Created April 24, 2014 04:53
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 hengkiardo/11241965 to your computer and use it in GitHub Desktop.
Save hengkiardo/11241965 to your computer and use it in GitHub Desktop.
How to format a number to string
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