Skip to content

Instantly share code, notes, and snippets.

@NaumenkoSergiy
Forked from tobyjsullivan/abbreviateNum.js
Created February 10, 2021 17:51
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 NaumenkoSergiy/d4720a3edf66269c94524643b0adccd7 to your computer and use it in GitHub Desktop.
Save NaumenkoSergiy/d4720a3edf66269c94524643b0adccd7 to your computer and use it in GitHub Desktop.
Abbreviate large numbers in Javascript
// Iterated from: https://stackoverflow.com/questions/10599933/convert-long-number-into-abbreviated-string-in-javascript-with-a-special-shortn
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