Files
gridpilot.gg/apps/website/lib/formatters/NumberFormatter.ts
Marc Mintel 046852703f
Some checks failed
Contract Testing / contract-tests (pull_request) Failing after 5m51s
Contract Testing / contract-snapshot (pull_request) Has been skipped
view data fixes
2026-01-24 12:14:08 +01:00

39 lines
951 B
TypeScript

/**
* NumberDisplay
*
* Deterministic number formatting for display.
* Avoids Intl and toLocaleString to prevent SSR/hydration mismatches.
*/
export class NumberFormatter {
/**
* Formats a number with thousands separators (commas).
* Example: 1234567 -> "1,234,567"
*/
static format(value: number): string {
const parts = value.toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
}
/**
* Formats a number in compact form (e.g., 1.2k, 1.5M).
*/
static formatCompact(value: number): string {
if (value >= 1000000) {
return `${(value / 1000000).toFixed(1)}M`;
}
if (value >= 1000) {
return `${(value / 1000).toFixed(1)}k`;
}
return value.toString();
}
/**
* Formats a number as currency.
*/
static formatCurrency(value: number, currency: string): string {
return `${currency} ${this.format(value)}`;
}
}