thelounge/client/js/helpers/roundBadgeNumber.ts
2022-12-22 23:12:24 +00:00

12 lines
361 B
TypeScript

export default (count: number) => {
if (count < 1000) {
return count.toString();
}
const suffixes = ["", "k", "M"];
const magnitudeIndex = Math.min(Math.floor(Math.log10(count) / 3), suffixes.length - 1);
const magnitude = 1000 ** magnitudeIndex;
const roundedCount = (count / magnitude).toFixed(1);
return roundedCount + suffixes[magnitudeIndex];
};