diff options
Diffstat (limited to 'frontend/lib/format.ts')
| -rw-r--r-- | frontend/lib/format.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/frontend/lib/format.ts b/frontend/lib/format.ts new file mode 100644 index 0000000..34bffd8 --- /dev/null +++ b/frontend/lib/format.ts @@ -0,0 +1,31 @@ +export function fmtCurrency(value?: number | null, decimals = 2): string { + if (value === null || value === undefined || Number.isNaN(value)) return "-"; + return `$${value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}`; +} + +export function fmtNumber(value?: number | null, decimals = 2): string { + if (value === null || value === undefined || Number.isNaN(value)) return "-"; + return value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals }); +} + +export function fmtLarge(value?: number | null): string { + if (value === null || value === undefined || Number.isNaN(value)) return "-"; + const abs = Math.abs(value); + const sign = value < 0 ? "-" : ""; + if (abs >= 1e12) return `${sign}$${(abs / 1e12).toFixed(2)}T`; + if (abs >= 1e9) return `${sign}$${(abs / 1e9).toFixed(2)}B`; + if (abs >= 1e6) return `${sign}$${(abs / 1e6).toFixed(1)}M`; + return `${sign}$${abs.toLocaleString(undefined, { maximumFractionDigits: 0 })}`; +} + +export function fmtPct(value?: number | null, decimals = 2, signed = false): string { + if (value === null || value === undefined || Number.isNaN(value)) return "-"; + const pct = value * 100; + const sign = signed && pct > 0 ? "+" : ""; + return `${sign}${pct.toFixed(decimals)}%`; +} + +export function deltaClass(value?: number | null): string { + if (value === null || value === undefined) return "neutral"; + return value >= 0 ? "positive" : "negative"; +} |
