UNPKG

@0xtorch/big-decimal

Version:

An arbitrary precision Decimal type for TypeScript that extends BigInt.

32 lines 948 B
import { removeTrailingZeros } from './removeTrailingZeros'; export const fix = ({ value, decimals }, fixDecimals, rounding) => { if (decimals <= fixDecimals) { return removeTrailingZeros({ value, decimals }); } const fixPlusOneValue = value / 10n ** BigInt(decimals - fixDecimals - 1); const fixValue = round(fixPlusOneValue, rounding); return removeTrailingZeros({ value: fixValue, decimals: fixDecimals, }); }; 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