Skip to content

Instantly share code, notes, and snippets.

@bockoblur
Forked from eugenemtn/abbreviatedNumber.ts
Last active May 16, 2023 19:12
Show Gist options
  • Save bockoblur/e359e73a5fffae7e6eeda9de0f86a9b9 to your computer and use it in GitHub Desktop.
Save bockoblur/e359e73a5fffae7e6eeda9de0f86a9b9 to your computer and use it in GitHub Desktop.
Formatter for abbrevated numbers
export function abbreviatedNumber(value: number, precision=2): string {
if (isNaN(value)) {
return "";
}
let newValue = value;
const suffixes = ["", "K", "M", "B", "T"];
let suffixNum = 0;
while (Math.abs(newValue) >= 1000) {
newValue /= 1000;
suffixNum++;
}
if (suffixNum >=suffixes.length)
return "∞";
else
return newValue.toPrecision(precision) + suffixes[suffixNum];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment