snort/src/Number.ts

15 lines
292 B
TypeScript
Raw Normal View History

2023-01-19 10:19:10 +00:00
const intl = new Intl.NumberFormat("en", {
minimumFractionDigits: 0,
maximumFractionDigits: 2,
});
2023-01-18 23:31:34 +00:00
export function formatShort(n: number) {
2023-02-04 13:30:05 +00:00
if (n < 2e3) {
2023-01-18 23:31:34 +00:00
return n
2023-02-07 13:32:32 +00:00
} else if (n < 1e6) {
2023-02-03 21:59:35 +00:00
return `${intl.format(n / 1e3)}K`
2023-01-18 23:31:34 +00:00
} else {
2023-02-03 21:59:35 +00:00
return `${intl.format(n / 1e6)}M`
2023-01-18 23:31:34 +00:00
}
}