Skip to main content
@spa-tools/utilities

formatQuotient()

The formatQuotient() function formats the quotient of a dividend using a default divisor of 1 million with multiple formattting options. This is especially useful for formatting large numbers into a more human-readable format.

Usage

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

const value = 1234567.890123;

const formattedValue1 = formatQuotient(value);
console.log(formattedValue);
// --> 1

const formattedValue2 = formatQuotient(value, { decimalPlaces: 3 });
console.log(formattedValue2);
// --> 1.235

const formattedValue3 = formatQuotient(value, { decimalPlaces: 1, unit: 'M' });
console.log(formattedValue3);
// --> 1.2M

const formattedValue4 = formatQuotient(value, { divisor: 1000, unit: 'K' });
console.log(formattedValue4);
// --> 1,235K

const formattedValue5 = formatQuotient(value, { divisor: 1000, useGrouping: false });
console.log(formattedValue5);
// --> 1235

Arguments

NameTypeRequired?DefaultDescription
dividendnumberyes-The dividend to use calculate the quotient
optionsFormatQuotientOptionsnosee FormatQuotientOptionsThe options for formatting quotient

Returns

Returns formatted quotient as string.

FormatQuotientOptions

The options object for the formatQuotient() function.

Properties

NameTypeRequired?DefaultDescription
decimalPlacesnumberfalse0The number of decimal places to round to
divisornumberfalse1000000The divisor to use to calculate the quotient
unitstringfalse''The unit to append to the result
useGroupingbooleanfalsetrueDetermines whether or not to add grouping (,)s to result