import type { ErrorType } from '../../errors/utils.js' export type ParseUnitsErrorType = ErrorType export function parseUnits(value: string, decimals: number) { let [integer, fraction = '0'] = value.split('.') const negative = integer.startsWith('-') if (negative) integer = integer.slice(1) // trim leading zeros. fraction = fraction.replace(/(0+)$/, '') // round off if the fraction is larger than the number of decimals. 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}`) }