Skip to content

formatCompact

Formats a number using compact notation (e.g. 1_500_000 → "1.5M").

Thin wrapper over Intl.NumberFormat with notation: 'compact'. Companion of formatSize in the same format* family.

Available since v2.0.0

import { formatCompact } from '@helpers4/number';
formatCompact(value: number, locale?: string): string
ParameterTypeDescription
valuenumberThe number to format.
localestringBCP 47 locale tag. Defaults to the runtime locale. (optional)

string — A compact string representation of the number.

Formats a number using K / M suffixes for readability.

formatCompact(1_500_000, 'en') // => '1.5M'
formatCompact(1_000, 'en')     // => '1K'
formatCompact(999, 'en')       // => '999'

Uses the provided locale for the decimal separator and suffix.

formatCompact(1_500_000, 'fr') // => '1,5 M'

View source on GitHub