Skip to main content
@spa-tools/utilities

humanizeUnit()

The humanizeUnit() function humanizes a number utilizing a square-basis unit, returning a tuple of formatted number with requested decimal places in first position and then respective human-readable unit in second position.

By default, it will use the following units: K, M, B, T and a basis of 1000 with one decimal place, respectively representing thousand, million, billion and trillion.

Usage

import { humanizeUnit } from '@spa-tools/utilities';

const humanizeUnit1 = humanizeUnit(1000000);
console.log(humanizeUnit1);
// --> ['1.00', 'M']

const humanizeUnit2 = humanizeUnit(1234567, { basis: 1024, decimalPlaces: 2, units: ['KB', 'MB', 'GB', 'TB'] });
console.log(humanizeUnit2);
// --> ['1.18', 'MB']

const humanizeUnit3 = humanizeUnit(1234567, {
basis: 1024,
decimalPlaces: 1,
units: ['KB', 'MB', 'GB', 'TB'],
useGrouping: false,
});
console.log(humanizeUnit3);
// --> ['1.2', 'MB']

const humanizeUnit4 = humanizeUnit(824, {
basis: 10,
decimalPlaces: 1,
units: ['decades', 'centuries', 'millennium', 'megayears', 'gigayears'],
useGrouping: false,
});
console.log(humanizeUnit4);
// --> ['8.2', 'centuries']

Arguments

NameTypeRequired?DefaultDescription
dividendnumberyes-The dividend to use calculate the unit
optionsHumanizeUnitOptionsnosee HumanizeUnitOptionsThe options for formatting the unit

Returns

Returns humanized value as string.

HumanizeUnitOptions

The options object for the humanizeUnit() function.

Properties

NameTypeRequired?DefaultDescription
basisnumberfalse1000The basis to use to calculate the unit
decimalPlacesnumberfalse0The number of decimal places to round to
unitsstring[]false['K', 'M', 'B', 'T']The square-based units to use