Files
gridpilot.gg/apps/website/lib/formatters/PercentFormatter.ts
2026-01-24 01:07:43 +01:00

26 lines
632 B
TypeScript

/**
* PercentDisplay
*
* Deterministic formatting for percentages.
*/
export class PercentFormatter {
/**
* Formats a decimal value as a percentage string.
* Example: 0.1234 -> "12.3%"
*/
static format(value: number | null | undefined): string {
if (value === null || value === undefined) return '0.0%';
return `${(value * 100).toFixed(1)}%`;
}
/**
* Formats a whole number as a percentage string.
* Example: 85 -> "85%"
*/
static formatWhole(value: number | null | undefined): string {
if (value === null || value === undefined) return '0%';
return `${Math.round(value)}%`;
}
}