Round big numbers with arbitrary precision.
npm install --save smart-round
The module exports a function that let you initialize a rounder function.
maxPrecision
: Amount of significant digits to return.
minDecimals
: Minimum amount of decimals to return.
maxDecimals
: Maximum amount of decimals to return.
input
: The value to round. Accepts any type supported by bignumber.js
package.
options
: An optional object to customize the formatting:
options.locale
: Optional. Format used to format whenoptions.shouldFormat
is true. Defaults toen-US
.options.roundingMode
Optional. Specifies the rounding method. Accepted values:round-up
,round-down
,round-ceil
,round-floor
,round-half-up
,round-half-down
,round-half-even
,round-half-ceil
,round-half-floor
. Defaults toround-half-up
.options.shouldFormat
: Optional. Defaults tofalse
. Indicates whether the string should be formatted using Intl.NumberFormat,
import { smartRound } from "smart-round";
const rounder1 = smartRound(6, 0, 6);
console.log(rounder1("1234.56789")); // '1234.57'
console.log(rounder1("1234.56789", { shouldFormat: true })); // '1,234.56'
const rounder2 = smartRound(4, 2, 6);
console.log(rounder2("1234", { shouldFormat: true })); // '1,234.00'
console.log(rounder2("0.000123456", { shouldFormat: true })); // '0.000123'