mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1export const formatCount = (num: number) =>
2 Intl.NumberFormat('en-US', {
3 notation: 'compact',
4 maximumFractionDigits: 1,
5 // `1,953` shouldn't be rounded up to 2k, it should be truncated.
6 // @ts-expect-error: `roundingMode` doesn't seem to be in the typings yet
7 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#roundingmode
8 roundingMode: 'trunc',
9 }).format(num)
10
11export function formatCountShortOnly(num: number): string {
12 if (num >= 1000000) {
13 return (num / 1000000).toFixed(1) + 'M'
14 }
15 if (num >= 1000) {
16 return (num / 1000).toFixed(1) + 'K'
17 }
18 return String(num)
19}