Currency - Format and convert currencies
Overview
The Currency
class provides utilities for handling currency operations including formatting and conversion. It supports locale-specific formatting and uses the Frankfurter API for currency conversion with automatic rate caching.
Constructor
Creates a new Currency instance with specified amount and currency code.
Signature
constructor(amount: Numeric, code: CurrencyCode)
Parameters
amount
: Numeric value (number or string) representing the currency amountcode
: ISO 4217 currency code (e.g., 'USD', 'EUR')
Behavior
- Converts amount to number
- Stores formatted currency string using 'en-US' locale
Example
new Currency(100, 'USD'); // $100.00
Available Methods
-
Static Methods
-
Instance Methods
Properties
currency
readonly currency: string
Pre-formatted currency string using 'en-US'
locale.
Examples
Basic Usage
const usd = new Currency(100, 'USD');
console.log(usd.currency); // "$100.00"
console.log(usd.format('ja-JP', 'JPY')); // "¥ 100"
Currency Conversion
const converted = await new Currency(100, 'USD').convert('EUR', {
fallbackRate: 0.85
});
console.log(converted.currency); // Current EUR equivalent
Error Handling
try {
const converted = await new Currency(100, 'USD').convert('XYZ');
} catch (error) {
console.error('Conversion failed:', error.message);
}