Skip to content

formatCurrency

Lower-level fixed-decimal currency formatter. Prefer formatCurrencyAmount for typical USD display unless you need an explicit decimals count.

ts
import { formatCurrency } from '@swapped/connect-sdk/format'

Decimals

Pads when the value has fewer places; truncates/rounds via toFixed when it has more. Default is 2.

ts
formatCurrency({ number: 12.3456 })
// "12.35"          — default 2 decimals

formatCurrency({ number: 12.3456789, decimals: 4 })
// "12.3457"        — more digits than `decimals` → shortened

formatCurrency({ number: 12.3456, decimals: 4 })
// "12.3456"        — exact match

formatCurrency({ number: 12.3, decimals: 4 })
// "12.3000"        — fewer digits → padded with zeros

formatCurrency({ number: 12, decimals: 4 })
// "12.0000"        — integer → fully padded

pretty vs js-number

ts
formatCurrency({ number: 1234567.891 })
// "1,234,567.89"   — pretty (default): thousand separators

formatCurrency({ number: 1234567.891, format: 'js-number' })
// "1234567.89"     — no separators (safer to parse / reuse in inputs)

Other cases

ts
formatCurrency({ number: 1.999, decimals: 2 })
// "2.00"           — uses `toFixed` before formatting

formatCurrency({ number: '99.999', decimals: 2 })
// "100.00"         — string input works the same
ParamDefaultPurpose
numberValue to format (number or numeric string)
decimals2Decimal places to keep
format'pretty''pretty' with thousand separators, or 'js-number' without

USD_CURRENCY_FORMAT ('$0,0.[00]') is also exported if you need a $-prefixed numeral pattern.