Skip to content

Instantly share code, notes, and snippets.

@eugenemtn
Created March 8, 2021 15:16
Show Gist options
  • Save eugenemtn/bda69d0a2ce409ed03d19ac1f8f96169 to your computer and use it in GitHub Desktop.
Save eugenemtn/bda69d0a2ce409ed03d19ac1f8f96169 to your computer and use it in GitHub Desktop.
Formatter for abbrevated numbers
export function abbreviatedNumber(value: number): string {
if (isNaN(value)) {
return "";
}
let newValue = value;
const suffixes = ["", "K", "M", "B", "T"];
let suffixNum = 0;
while (newValue >= 1000) {
newValue /= 1000;
suffixNum++;
}
return newValue.toPrecision(3) + suffixes[suffixNum];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment