Skip to content

Instantly share code, notes, and snippets.

@edazpotato
Forked from tobyjsullivan/abbreviateNum.js
Created December 30, 2020 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edazpotato/5780e4cc0ce6318dd32736fd4d3f2628 to your computer and use it in GitHub Desktop.
Save edazpotato/5780e4cc0ce6318dd32736fd4d3f2628 to your computer and use it in GitHub Desktop.
Abbreviate large numbers in Javascript (as a es6 module)
export default function abbreviateNumber(value) {
let newValue = value;
const suffixes = ["", "K", "M", "B","T"];
let suffixNum = 0;
while (newValue >= 1000) {
newValue /= 1000;
suffixNum++;
}
newValue = newValue.toPrecision(3);
newValue += suffixes[suffixNum];
return newValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment