Skip to content

formatAbbreviate

Formats a number to an appropriate number of decimal places and rounding, adding suffixes if applicable (ie. 1200000 to "1.2M").

Rounds each number to about three significant digits and appends an SI-style suffix, so 1234 reads as 1.23k, 1234567 as 1.23M, and 1234567890 as 1.23B, while a small fraction like 0.0042 is left unabbreviated.

import {formatAbbreviate} from "@d3plus/format";

formatAbbreviate(42);
formatAbbreviate(1234);
formatAbbreviate(1234567);
formatAbbreviate(1234567890);
formatAbbreviate(0.0042);

Parameters

import {formatAbbreviate} from "@d3plus/format";
nrequiredstring | number

The number to be formatted.

localestring | FormatLocaleDefinitionDefault en-US

The locale config to be used. If an object is provided, the function will format the numbers according to the object. The object must include suffixes, delimiter and currency properties.

precisionstring

Number of significant digits to display.