Skip to main content

Currency

Money for display — formatting prices and compact notation, parsing them back, and looking up symbols, names and locales for ISO 4217 codes. Formatting delegates to Intl.NumberFormat, so symbol placement, digit counts and grouping come from the platform rather than a bundled table, and the currency-name list is loaded lazily to keep the module small. Amounts are plain numbers: fine to render, wrong for accounting arithmetic, where integer minor units or a decimal library belong.

Example

import { formatPrice, formatPriceCompact, parsePrice } from '@rtorcato/js-common/currency'

// Render a price, then read one back out of a text input.
formatPrice(1234.56, 'USD') // '$1,234.56'
formatPrice(1234.56, 'EUR', 'de-DE') // '1.234,56 €'
formatPriceCompact(1234567, 'USD') // '$1.2M' — for dashboards and charts

parsePrice('$1,234.56') // 1234.56
parsePrice('€1.234,56') // 1234.56 — European grouping handled too
parsePrice('free') // null

Import

import { convertCurrency, formatPrice, formatPriceCompact } from '@rtorcato/js-common/currency'

Exports

NameSummary
convertCurrencyConverts a given amount from one currency to another using the provided exchange rate.
formatPriceFormats a given price value into a localized currency string.
formatPriceCompactFormats a price in compact notation (e.g., $1.2K, $1.5M).
getCurrencyLocaleReturns the default locale typically associated with a currency.
getCurrencyNameReturns the full display name of a currency.
getCurrencySymbolReturns the currency symbol for a given ISO 4217 currency code.
isValidCurrencySynchronously checks if a currency code is valid using the Intl API.
isValidCurrencyCodeChecks if the provided currency code is valid.
parseCurrencyStringParses a currency string and extracts both the amount and currency code.
parsePriceParses a given price string and returns its numeric value.

See also

  • numbers — sum, average, clamp, roundTo
  • i18n — locale-aware number/date formatting and translation