Created
March 17, 2023 21:12
-
-
Save omar2205/2412c59fcc9851ad1eb9ef5d338e9d03 to your computer and use it in GitHub Desktop.
Format number
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 formatNumber(num: number): string { | |
if (num < 1000) return num.toString() | |
// If the number is between 1000 and 999999, divide it by 1000 and append K | |
else if (num >= 1000 && num <= 999999) { | |
return (num / 1000).toFixed(1) + "K" | |
} | |
// If the number is greater than or equal to 1000000, divide it by 1000000 and append mil | |
else { | |
return (num / 1000000).toFixed(1) + "M" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment