diff options
| author | Tyler Hoang <tyler@tylerhoang.xyz> | 2026-06-29 01:41:26 -0700 |
|---|---|---|
| committer | Tyler Hoang <tyler@tylerhoang.xyz> | 2026-06-29 01:41:26 -0700 |
| commit | ca54e9313894600feac548b13da8fab335db2905 (patch) | |
| tree | 7eff74d8d77eecbbf68640d3bbb5776db5cdbb69 /frontend/lib/format.ts | |
| parent | 53e7bce26a21ab3853712ab53a38c6760b4ab25a (diff) | |
feat: update frontend API types, client and formatters
Diffstat (limited to 'frontend/lib/format.ts')
| -rw-r--r-- | frontend/lib/format.ts | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/frontend/lib/format.ts b/frontend/lib/format.ts index 6a1fcc2..e4bae13 100644 --- a/frontend/lib/format.ts +++ b/frontend/lib/format.ts @@ -1,6 +1,22 @@ -export function fmtCurrency(value?: number | null, decimals = 2): string { +const CURRENCY_SYMBOLS: Record<string, string> = { + USD: "$", + CAD: "C$", + GBP: "£", + EUR: "€", + JPY: "¥", + AUD: "A$", + CHF: "CHF ", + CNY: "CN¥", + HKD: "HK$", +}; + +function currencySymbol(currency = "USD"): string { + return CURRENCY_SYMBOLS[currency.toUpperCase()] || `${currency.toUpperCase()} `; +} + +export function fmtCurrency(value?: number | null, decimals = 2, currency = "USD"): string { if (value === null || value === undefined || Number.isNaN(value)) return "-"; - return `$${value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}`; + return `${currencySymbol(currency)}${value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals })}`; } export function fmtNumber(value?: number | null, decimals = 2): string { @@ -8,14 +24,15 @@ export function fmtNumber(value?: number | null, decimals = 2): string { return value.toLocaleString(undefined, { maximumFractionDigits: decimals, minimumFractionDigits: decimals }); } -export function fmtLarge(value?: number | null): string { +export function fmtLarge(value?: number | null, currency = "USD"): 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 })}`; + const symbol = currencySymbol(currency); + if (abs >= 1e12) return `${sign}${symbol}${(abs / 1e12).toFixed(2)}T`; + if (abs >= 1e9) return `${sign}${symbol}${(abs / 1e9).toFixed(2)}B`; + if (abs >= 1e6) return `${sign}${symbol}${(abs / 1e6).toFixed(1)}M`; + return `${sign}${symbol}${abs.toLocaleString(undefined, { maximumFractionDigits: 0 })}`; } export function fmtPct(value?: number | null, decimals = 2, signed = false): string { |
