The amount to convert
The target currency code (e.g., 'EUR', 'GBP')
The converted amount in the target currency, formatted to 2 decimal places
// Convert 100 USD to EUR
await USDto(100, 'EUR')
// Returns 85.32 (if rate is 0.8532)
// Convert 500 USD to GBP
await USDto(500, 'GBP')
// Returns 387.50 (if rate is 0.775)
export async function USDto(amount: number, toCurrencyCode: CurrencyCode): Promise<number> {
const currencyData = priceParser.currencies.find(
(c: { code: string }) => c.code === toCurrencyCode.toLowerCase(),
);
if (!currencyData) return 0;
const rate = await getCurrencyRate("USD", toCurrencyCode);
return parseFloat(Number(amount * rate).toFixed(currencyData.exponent));
}
Converts a given amount from USD to any supported currency. Uses real-time exchange rates from the Hexarate API. Results are rounded to 2 decimal places for standard currency format.