Files
orca/src/shared/currency-format.ts
T
Neilandmmarabel 2b0ce17514 Show provider credit balances alongside usage limits (#25408)
Display provider credit and currency balances separately from rate limits. Preserve quotas when balance data is unavailable and translate complete labels.

Supersedes #9363; credit to @mmarabel for the original implementation.

Co-authored-by: mmarabel <166927047+mmarabel@users.noreply.github.com>
2026-10-05 00:07:23 -07:00

22 lines
799 B
TypeScript

/**
* Format a major-unit amount (e.g. 12.4) as a localized currency string
* (e.g. "$12.40"). Shared by the usage popover and the compact status-bar
* balance token so both render identically.
*
* Why: `Intl.NumberFormat` throws on an unrecognized currency code (the code
* comes from a provider API we don't control), so fall back to a plain
* "<CODE> <amount>" rendering rather than crashing the status bar.
*/
export function formatCurrencyAmount(amount: number, currencyCode: string): string {
const safeAmount = Number.isFinite(amount) ? amount : 0
const code = currencyCode.trim() || 'USD'
try {
return new Intl.NumberFormat(undefined, {
style: 'currency',
currency: code
}).format(safeAmount)
} catch {
return `${code} ${safeAmount.toFixed(2)}`
}
}