@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
Name | Type | Required? | Default | Description |
---|---|---|---|---|
dividend | number | yes | - | The dividend to use calculate the quotient |
options | FormatQuotientOptions | no | see FormatQuotientOptions | The options for formatting quotient |
Returns
Returns formatted quotient as string
.
FormatQuotientOptions
The options object for the formatQuotient()
function.
Properties
Name | Type | Required? | Default | Description |
---|---|---|---|---|
decimalPlaces | number | false | 0 | The number of decimal places to round to |
divisor | number | false | 1000000 | The divisor to use to calculate the quotient |
unit | string | false | '' | The unit to append to the result |
useGrouping | boolean | false | true | Determines whether or not to add grouping (,)s to result |