-
-
Save bockoblur/e359e73a5fffae7e6eeda9de0f86a9b9 to your computer and use it in GitHub Desktop.
Formatter for abbrevated numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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