JavaScript theorytheory 0/50 · 0%
Numbers · medium

33. Numbers, BigInt and token amounts

Why Number breaks on wei.

JavaScript numbers are 64-bit floats: exact only up to 2^53-1. Token amounts in wei exceed that, so use BigInt (`123n`) for on-chain values and format only for display.

const wei = 1_500000000000000000n;
const display = Number(wei / 10n ** 15n) / 1000; // 1.5

Never mix BigInt and Number in arithmetic — that throws. Convert deliberately at the display boundary.

Check your understanding

  1. 1. What is the safe integer limit?

  2. 2. What happens with `1n + 1`?

  3. 3. Where should formatting happen?