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

Syntax

TypeScript
import { formatCurrency } from '@opentf/std';

formatCurrency(value: number, currency: string, options?: {
  display?: 'symbol' | 'code' | 'name';
  minFraction?: number;
  maxFraction?: number;
  locale?: string;
}): string

Parameters

NameTypeDescription
valuenumberThe number to format.
currencystringThe ISO 4217 currency code (e.g., 'USD', 'EUR').
optionsobjectThe options object.
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).

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, 'USD') //=> '$1,200.00'

formatCurrency(1200, 'EUR') //=> '€1,200.00'

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

formatCurrency(1200, 'INR') //=> '₹1,200.00'

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

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

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

formatCurrency(1200, 'USD', { maxFraction: 0 }) //=> '$1,200'
Last updated on
Edit this page