In this track: Advanced security reasoning
advanced · 4 MIN READ

Precision loss across decimals

Conversion order and rounding can destroy small values.

LET’S PICTURE IT

Changing measuring rulers repeatedly can discard fractions at every step.

SEE IT FOR YOURSELF
The same native ether amount shown as ETH, Gwei and WeiETH1× 1 billionGWEI1000000000× 1 billionWEI1000000000000000000

1 ETH = 1000000000 Gwei = 1000000000000000000 Wei.

Same value · different units · native ether only

Track the unit of every integer

A raw amount at six decimals differs from the same displayed amount at eighteen decimals. Carry the scale through each formula explicitly.

Avoid early truncation

Dividing before multiplying can discard information that a later multiplication cannot restore. Reordering also needs overflow analysis or suitable full-precision arithmetic.

Test small and extreme values

Tiny nonzero amounts, maximum supported amounts and mixed-scale ratios reveal different problems. Include repeated conversion sequences.

Specify acceptable loss

Some rounding is unavoidable with integer units. Define who bears it and bound the allowed error under the application’s accounting rules.

Work through the boundary

floor(1 / 10) × 100 = 0
floor(1 × 100 / 10) = 10

Check overflow and intended units before choosing a real formula.

A simplified local review scenario. The example establishes a question to test, not a guarantee of production safety.

QUICK CHECK · NO PRESSURE

Can later multiplication recover information discarded by earlier integer division?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.