-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84c91f4
commit 8297057
Showing
5 changed files
with
56 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
export const formatNumberShorthand = (num: number) => { | ||
if (num > 999 && num <= 999999) { | ||
return (num / 1000).toFixed(1) + 'K'; | ||
const formattedNum = (num / 1000).toFixed(1); | ||
// Remove decimal if the 1000th place is a zero | ||
return formattedNum.endsWith('.0') ? `${ formattedNum.slice(0, -2) }K` : `${ formattedNum }K`; | ||
} else if (num > 999999) { | ||
return (num / 1000000).toFixed(1) + 'M'; | ||
const formattedNum = (num / 1000000).toFixed(1); | ||
// Remove decimal if the millionth place is a zero | ||
return formattedNum.endsWith('.0') ? `${ formattedNum.slice(0, -2) }M` : `${ formattedNum }M`; | ||
} else { | ||
return num.toString(); | ||
let formattedNum = num.toString(); | ||
const decimalIndex = formattedNum.indexOf('.'); | ||
// If the number has more than two decimal places, remove the extra ones | ||
if (decimalIndex !== -1 && formattedNum.length > decimalIndex + 3) { | ||
formattedNum = formattedNum.slice(0, decimalIndex + 3); | ||
} | ||
return formattedNum; | ||
} | ||
}; |