2.0.0 • Published 8 months ago
smart-round v2.0.0
smart-round
Round big numbers with arbitrary precision.
Installation
npm install --save smart-roundUsage
The module exports a function that let you initialize a rounder function.
smartRound(maxPrecision, minDecimals, maxDecimals)
maxPrecision: Amount of significant digits to return.
minDecimals: Minimum amount of decimals to return.
maxDecimals: Maximum amount of decimals to return.
rounder(number, shouldFormat)
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.shouldFormatis true. Defaults toen-US.options.roundingModeOptional. 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,
Example
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'