UNPKG

viem

Version:

TypeScript Interface for Ethereum

40 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseUnits = parseUnits; const unit_js_1 = require("../../errors/unit.js"); function parseUnits(value, decimals) { if (!/^(-?)([0-9]*)\.?([0-9]*)$/.test(value)) throw new unit_js_1.InvalidDecimalNumberError({ value }); let [integer, fraction = '0'] = value.split('.'); const negative = integer.startsWith('-'); if (negative) integer = integer.slice(1); fraction = fraction.replace(/(0+)$/, ''); if (decimals === 0) { if (Math.round(Number(`.${fraction}`)) === 1) integer = `${BigInt(integer) + 1n}`; fraction = ''; } else if (fraction.length > decimals) { const [left, unit, right] = [ fraction.slice(0, decimals - 1), fraction.slice(decimals - 1, decimals), fraction.slice(decimals), ]; const rounded = Math.round(Number(`${unit}.${right}`)); if (rounded > 9) fraction = `${BigInt(left) + BigInt(1)}0`.padStart(left.length + 1, '0'); else fraction = `${left}${rounded}`; if (fraction.length > decimals) { fraction = fraction.slice(1); integer = `${BigInt(integer) + 1n}`; } fraction = fraction.slice(0, decimals); } else { fraction = fraction.padEnd(decimals, '0'); } return BigInt(`${negative ? '-' : ''}${integer}${fraction}`); } //# sourceMappingURL=parseUnits.js.map