summaryrefslogtreecommitdiff
path: root/frontend/lib/format.ts
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/lib/format.ts')
-rw-r--r--frontend/lib/format.ts31
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 {