export interface BigIntToNumberOptions { /** * The number of digits starting from the right of `value` to consider as decimal places. * * @default 18n */ precision?: bigint | undefined } /** * Converts a bigint to a number. * * @param value - The bigint to convert to a number. * @param options - Conversion options. * @returns The number representation of `value` */ export function bigIntToNumber(value: bigint, options?: BigIntToNumberOptions): number { const { precision = 18n } = options ?? {} if (precision < 0n) { throw new Error(`[bigIntToNumber] \`precision\` must be positive, got ${precision}`) } if (precision === 0n) { return Number(value) } else { const valueString = String(value) const decimalPointIndex = valueString.length - Number(precision) if (decimalPointIndex <= 0) { return Number(`0.${'0'.repeat(Math.abs(decimalPointIndex))}${valueString}`) } return Number( `${valueString.slice(0, decimalPointIndex)}.${valueString.slice(decimalPointIndex)}`, ) } }