The amount to convert
The source currency code (e.g., 'EUR', 'GBP')
The converted amount in USD, formatted to 2 decimal places
// Convert 100 EUR to USD
await toUSD(100, 'EUR')
// Returns 118.45 (if rate is 1.1845)
// Convert 1000 JPY to USD
await toUSD(1000, 'JPY')
// Returns 9.12 (if rate is 0.00912)
// Chain conversions
const price = parsePrice('€50.00');
if (price) {
const usdAmount = await toUSD(price.price, price.currencyCode);
}
export async function toUSD(amount: number, fromCurrencyCode: CurrencyCode): Promise<number> {
const rate = await getCurrencyRate(fromCurrencyCode, "USD");
return parseFloat(Number(amount * rate).toFixed(2));
}
Converts a given amount from any supported currency to USD. Uses real-time exchange rates from the Hexarate API. Results are rounded to 2 decimal places for standard currency format.