formatCurrency

💰 Formats a number as a currency string using Intl.NumberFormat.

Syntax

TypeScript
import { formatCurrency } from '@opentf/std';
formatCurrency(value: number, options: {
  currency: string;
  display?: 'symbol' | 'code' | 'name';
  minFraction?: number;
  maxFraction?: number;
  locale?: string;
}): string

Parameters

NameTypeDescription
valuenumberThe number to format.
optionsobjectThe options object.
options.currencystringThe ISO 4217 currency code (e.g., 'USD', 'EUR').
options.display'symbol' | 'code' | 'name'The display format (default 'symbol').
options.minFractionnumberThe minimum fraction digits.
options.maxFractionnumberThe maximum fraction digits.
options.localestringThe locale (default runtime locale).

options.currency must be a 3-letter ISO 4217 code. Fraction digit options must be integers between 0 and 100, and minFraction must not exceed maxFraction. options.display must be one of 'symbol', 'code', or 'name'.

Returns

The formatted currency string.

Examples

TypeScript
formatCurrency(1200, { currency: 'USD' }) //=> '$1,200.00'

formatCurrency(1200, { currency: 'EUR' }) //=> '€1,200.00'

formatCurrency(1200, { currency: 'JPY' }) //=> '¥1,200' (no decimals, Intl knows)

formatCurrency(1200, { currency: 'INR' }) //=> '₹1,200.00'

formatCurrency(1200, { currency: 'EUR', locale: 'de-DE' }) //=> '1.200,00 €'

formatCurrency(1200, { currency: 'USD', display: 'code' }) //=> 'USD 1,200.00'

formatCurrency(1200, { currency: 'USD', display: 'name' }) //=> '1,200.00 US dollars'

formatCurrency(1200, { currency: 'USD', maxFraction: 0 }) //=> '$1,200'

Try it

Last updated on
Edit this page