smart-round
Version:
Round big numbers with arbitrary precision
63 lines • 2.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.smartRound = void 0;
const big_js_1 = __importDefault(require("big.js"));
const roundingModes_js_1 = require("./roundingModes.js");
const getDecimalPlaces = (bigJsNumber) => bigJsNumber.toFixed().split(".")[1]?.length ?? 0;
const parse = function ({ input, maxDecimals, maxPrecision, minDecimals, }) {
if (maxPrecision <= 0) {
throw new Error("maxPrecision should be positive larger than 1");
}
if (minDecimals < 0) {
throw new Error("minDecimals should be positive");
}
if (maxDecimals < 0) {
throw new Error("maxDecimals should be positive");
}
if (minDecimals > maxDecimals) {
throw new Error("minDecimals should be larger than maxDecimals");
}
if (typeof input === "number" && (isNaN(input) || !isFinite(input))) {
throw new Error("input should be a valid number");
}
try {
const bigJsNumber = new big_js_1.default(input.toString().replaceAll(",", ""));
return bigJsNumber;
}
catch {
throw new Error("input should be a valid number");
}
};
const smartRound = (maxPrecision, minDecimals, maxDecimals) => function (input, options) {
const { locale = "en-US",
// Big Number default's, but just to make it explicit
roundingMode = "round-half-up", shouldFormat = false, } = options ?? {};
const bigJsNumber = parse({
input,
maxDecimals,
maxPrecision,
minDecimals,
});
// Calculate how much to reduce the precision
// The array of digits is the precision
const adjustment = Math.max(bigJsNumber.c.length - maxPrecision, 0);
// Calculate how much to reduce the decimals
const decimals = Math.max(getDecimalPlaces(bigJsNumber) - adjustment, minDecimals);
const newDecimals = Math.min(decimals, maxDecimals);
const string = bigJsNumber.toFixed(newDecimals, (0, roundingModes_js_1.toBigJsRoundingModes)(roundingMode));
if (!shouldFormat) {
return string;
}
// formats using the ","
return new Intl.NumberFormat(locale, {
maximumFractionDigits: newDecimals,
minimumFractionDigits: newDecimals,
useGrouping: true,
// @ts-expect-error NumberFormat.format accept strings, typings are wrong. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/format#parameters
}).format(string);
};
exports.smartRound = smartRound;
//# sourceMappingURL=index.js.map