Skip to content

formatCompactNumber

Formats a number into a compact lowercase abbreviation (e.g. 1.5k, 2m). Returns '' when the input is not a finite number.

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

Defaults

ts
formatCompactNumber(999)
// "999"            — under 1000 stays plain

formatCompactNumber(1000)
// "1k"

formatCompactNumber(1500)
// "1.5k"           — non-zero remainder → one decimal

formatCompactNumber(10000)
// "10k"

formatCompactNumber(1_500_000)
// "2m"             — numeral compact rounding

Custom pattern / invalid input

ts
formatCompactNumber(1500, '0.00a')
// "1.50k"          — force two decimal places in the abbreviation

formatCompactNumber(undefined)
// ""
ParamPurpose
numberValue to format; non-numbers yield ''
customFormatOptional numeral format override (e.g. '0.00a')

Without customFormat, the pattern is chosen via getCompactNumberFormat ('0.0a' vs '0a').

For uppercase token-style abbreviations (12.50K), see formatCompactTokenAmount.