Skip to main content

Try it Live

Run Denomination examples in the interactive playground

Conversions

Type-safe conversions between Wei, Gwei, and Ether denominations.

Overview

Each denomination type provides conversion methods to other units. All conversions use integer arithmetic - fractional values truncate (round down).

Conversion Table

FromToFunctionFormulaExample
WeiGweiWei.toGwei(wei)wei / 10^91_000_000_000n Wei → 1n Gwei
WeiEtherWei.toEther(wei)wei / 10^181_000_000_000_000_000_000n Wei → 1n Ether
GweiWeiGwei.toWei(gwei)gwei * 10^91n Gwei → 1_000_000_000n Wei
GweiEtherGwei.toEther(gwei)gwei / 10^91_000_000_000n Gwei → 1n Ether
EtherWeiEther.toWei(ether)ether * 10^181n Ether → 1_000_000_000_000_000_000n Wei
EtherGweiEther.toGwei(ether)ether * 10^91n Ether → 1_000_000_000n Gwei

Wei Conversions

Formula: gwei = wei / 1_000_000_000 (integer division)

Gwei Conversions

Formula: wei = gwei * 1_000_000_000

Ether Conversions

Formula: wei = ether * 1_000_000_000_000_000_000

Conversion Patterns

Gas Price Calculation

Balance Display

Multi-Step Conversion

Comparing Different Units

Integer Division Behavior

Division operations truncate fractional results. Always convert to smaller units before calculations requiring precision.

Truncation Examples

Avoiding Truncation

Round-Trip Safety

Safe Round Trips (No Loss)

Unsafe Round Trips (Loss)

Best Practices

1. Convert to Smallest Unit Early

2. Use Type System for Safety

3. Display in Appropriate Unit

Conversion Constants

All conversions based on these constants:

WASM Acceleration

All conversions available in WebAssembly:
See WASM for details.