@0xtorch/big-decimal
Version:
An arbitrary precision Decimal type for TypeScript that extends BigInt.
36 lines • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fix = void 0;
const removeTrailingZeros_1 = require("./removeTrailingZeros");
const fix = ({ value, decimals }, fixDecimals, rounding) => {
if (decimals <= fixDecimals) {
return (0, removeTrailingZeros_1.removeTrailingZeros)({ value, decimals });
}
const fixPlusOneValue = value / 10n ** BigInt(decimals - fixDecimals - 1);
const fixValue = round(fixPlusOneValue, rounding);
return (0, removeTrailingZeros_1.removeTrailingZeros)({
value: fixValue,
decimals: fixDecimals,
});
};
exports.fix = fix;
const round = (value, rounding) => {
switch (rounding) {
case 'ceil': {
if (value < 0n) {
return (value - 9n) / 10n;
}
return (value + 9n) / 10n;
}
case 'floor': {
return value / 10n;
}
case 'round': {
if (value < 0n) {
return (value - 5n) / 10n;
}
return (value + 5n) / 10n;
}
}
};
//# sourceMappingURL=fix.js.map