Skip to content

formatCurrencyAmount

Primary helper for USD-style amounts (mins, maxes, wallet value). Wraps formatCurrency with smarter defaults for small values and trailing zeros.

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

Defaults (2 decimals, pretty)

ts
formatCurrencyAmount(0)
// "0.00"

formatCurrencyAmount(12)
// "12.00"

formatCurrencyAmount(12.3)
// "12.30"

formatCurrencyAmount(12.345)
// "12.35"

formatCurrencyAmount(1234.5)
// "1,234.50"

format: 'js-number'

ts
formatCurrencyAmount(1234.5, { format: 'js-number' })
// "1234.50"        — no thousand separators (good for inputs)

Tiny amounts (firstNonZeroDecimal)

Without the flag, values under 0.01 collapse to "0.00". With it, precision expands until the first significant digit is visible.

ts
formatCurrencyAmount(0.004)
// "0.00"

formatCurrencyAmount(0.004, { firstNonZeroDecimal: true })
// "0.004"

formatCurrencyAmount(0.00004)
// "0.00"

formatCurrencyAmount(0.00004, { firstNonZeroDecimal: true })
// "0.00004"

formatCurrencyAmount(0.0001, { firstNonZeroDecimal: true })
// "0.0001"

formatCurrencyAmount('0.0000001', { firstNonZeroDecimal: true })
// "0.0000001"

formatCurrencyAmount(1e-7, { firstNonZeroDecimal: true })
// "0.0000001"      — scientific notation is expanded first

Trailing zeros (removeTrailingZeros)

ts
formatCurrencyAmount('1.50')
// "1.50"

formatCurrencyAmount('1.50', { removeTrailingZeros: true })
// "1.5"

formatCurrencyAmount('1.5000', { removeTrailingZeros: true })
// "1.5"

formatCurrencyAmount(1000.1, { removeTrailingZeros: true })
// "1,000.1"

Invalid input

Whitespace, grouped numbers, NaN, and other non-amounts return ''. Scientific notation is expanded to a plain decimal. Pass a JS number or a plain decimal string (23, 1.234). Use format: 'js-number' when the result will be parsed again.

ts
formatCurrencyAmount(' ')
// ""

formatCurrencyAmount('1,234.50')
// ""
OptionDefaultPurpose
format'pretty''pretty' adds thousand separators; 'js-number' does not
decimalsfiatDecimals (2)Fixed decimal places. Per-call value wins over setFormatDefaults
firstNonZeroDecimalfalseExpand past 2 decimals so tiny values are visible
removeTrailingZerosfalseStrip trailing zeros after the decimal

Prefer this over formatCurrency unless you only need a fixed decimals count.