Toolbox-XToolbox-X

Currency

Currency class for locale-specific currency formatting and Frankfurter API-based currency conversion.

Currency

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.

Import

import { Currency } from 'toolbox-x';

Constructor

Creates a new Currency instance with specified amount and currency code.

Signature

constructor<Code extends CurrencyCode>(amount: Numeric, code: Code): Currency<Code>

Parameters

ParameterTypeDescription
amountNumericNumeric value (number or string) representing the currency amount.
codeCodeISO 4217 currency code (e.g., 'USD', 'EUR').

Behavior

  • Converts amount to a number.
  • Stores a pre-formatted currency string using the 'en-US' locale.

Example

playground.ts

Available Methods


Properties

currency

readonly currency: string

Pre-formatted currency string using the 'en-US' locale.


Examples

Basic Usage

playground.ts

Currency Conversion

import { Currency } from 'toolbox-x';

const usd = new Currency(100, 'USD');
const converted = await usd.convert('EUR', {
  fallbackRate: 0.85
});
console.log(converted.currency); // → Current EUR equivalent

Error Handling

import { Currency } from 'toolbox-x';

try {
  const converted = await new Currency(100, 'USD').convert('XYZ' as any);
} catch (error: any) {
  console.error('Conversion failed:', error.message);
}

API Reference for Currency

format()

Formats the currency amount according to specified locale rules.

Signature

format(locale?: LocaleCode, code?: CurrencyCode): string

Parameters

ParameterTypeDescription
localeLocaleCode (optional)BCP 47 locale code (e.g., 'de-DE').
codeCurrencyCode (optional)ISO 4217 currency code (e.g., 'USD', 'EUR') used solely for formatting purposes. This does not alter the internal currency code set during instantiation.

Return Value

  • string: Formatted currency string.

Example

playground.ts

convert()

Converts the current currency amount to a target currency using real-time exchange rates from api.frankfurter.app.
Includes automatic caching, error fallback handling, and support for manually defined rates. Refer to convertSync for a synchronous and network-independent solution with a manual exchange rate.

Signature

async convert<To extends FrankFurterCurrency>(to: To, options?: ConvertOptions): Promise<Currency<To>>

Parameters

ParameterTypeDescription
toToTarget currency code to convert to. Must be a valid Supported Currency, e.g., 'EUR', 'USD'.
optionsConvertOptions (optional)Configuration options.

Return Value

  • Promise<Currency<To>>: A new Currency instance containing the converted amount in the target currency.

Throws

Throws an error if:

  • The API call fails and
  • No fallbackRate is provided in options.

Behavior Details

  • Caches conversion rates internally to avoid redundant API requests.
  • Respects forceRefresh to bypass the cache when needed.
  • Falls back to the provided fallbackRate if the live API fails or the currency is unsupported.
  • Logs a warning in the console when falling back to a manual rate.

Supported Currencies

Only the following fiat currencies are supported by the live API:

AUD, BGN, BRL, CAD, CHF, CNY, CZK, DKK, EUR, GBP, HKD, HUF,
IDR, ILS, INR, ISK, JPY, KRW, MXN, MYR, NOK, NZD, PHP, PLN,
RON, SEK, SGD, THB, TRY, USD, ZAR

Use the convertSync method to convert to other currencies using custom exchange rates.

Example

import { Currency } from 'toolbox-x';

const usd = new Currency(100, 'USD');

// Convert to EUR using live rates
const eur = await usd.convert('EUR');

// Convert with fallback if API fails
const inr = await usd.convert('INR', {
  fallbackRate: 83.12,
});

convertSync()

Converts currency synchronously using either a cached rate or a manually provided exchange rate.
No network requests are made.

Signature

convertSync<To extends CurrencyCode>(to: To, rate: number): Currency<To>

Parameters

ParameterTypeDescription
toToTarget currency code.
ratenumberManual exchange rate to use.

Return Value

  • Currency<To>: A new Currency instance with the converted amount.
  • If no cached rate is found, the given manual rate is used.
  • If no exchange rate is valid, returns the original currency instance.

Example

playground.ts

clearRateCache()

Clears all cached exchange rates to force fresh API calls.

Signature

static clearRateCache(): void

Example

playground.ts

Type Definitions

LocaleCode

Supported BCP 47 locale codes for formatting.

CurrencyCode

Union of all supported currency codes in your system, including ISO 4217 and custom mappings.

FrankFurterCurrency

Subset of CurrencyCode that are officially supported by the Frankfurter API.

ConvertOptions

interface ConvertOptions {
  fallbackRate?: number;
  forceRefresh?: boolean;
}

Last updated: Mon, Jun 15, 2026 06:09:51AM (UTC)

On this page