Fundamentals·5 min read

wei, gwei, ether: getting units right in production

The single most common bug in crypto backends is a unit mistake — displaying wei as ether, or worse, sending it. The fix is boring and absolute: never use floating point for on-chain amounts, and always know which unit a number is in.

The ladder

Ether has 18 decimals. 1 ether = 10^9 gwei = 10^18 wei. Gwei (10^9 wei) is the unit gas prices are quoted in; wei is the base unit every RPC value is denominated in. Bitcoin's equivalent is the satoshi (10^-8 BTC), TRON's is the sun (10^-6 TRX), TON's is the nanoton (10^-9 TON). Every chain has a base integer unit — work in it.

Why floats break

IEEE-754 doubles have 53 bits of mantissa, about 15–16 significant decimal digits. A wei value can have 19+ digits, so the moment you parse it into a JavaScript number you lose precision — silently. The rounding error is tiny in relative terms and catastrophic in a ledger. Use native BigInt, or a big-decimal library, end to end.

Convert only at the edges

Keep amounts as base-unit integers everywhere in your system — storage, arithmetic, comparisons. Convert to a human-readable decimal string only at the display boundary, and parse user input back to the base unit immediately on the way in. If a float ever touches an amount between those two edges, treat it as a bug.

1st Node Engineering

Частые вопросы

Is it safe to use JavaScript numbers for small amounts?

No — draw the line at type, not size. Once any code path uses floats for amounts, a large value will eventually flow through it. Use BigInt for all on-chain amounts regardless of magnitude.

How do I display a wei value as ether without a library?

Divide by 10^18 using integer/BigInt math for the whole part and format the remainder as the fractional digits — never via floating-point division. Our unit converter tools do exactly this.

Related

Пополнили, получили ключ, запустили.

Полное самообслуживание. Оплата криптой или картой. Тарификация в кредитах — тяжёлые примитивы дороже, простые дёшевы.

Получить API-ключ