Skip to content

Instantly share code, notes, and snippets.

@omar2205
Created March 17, 2023 21:12
Show Gist options
  • Save omar2205/2412c59fcc9851ad1eb9ef5d338e9d03 to your computer and use it in GitHub Desktop.
Save omar2205/2412c59fcc9851ad1eb9ef5d338e9d03 to your computer and use it in GitHub Desktop.
Format number
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