In this track: Smart contract security foundations
advanced · 4 MIN READ

Rounding direction in financial code

Integer rounding can shift value between participants.

LET’S PICTURE IT

Cutting every fractional coin in the same direction can favor one side repeatedly.

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

Identify the exact ratio

Write the numerator, denominator and units before evaluating an integer formula. Multiplying and dividing in a different order can change precision or overflow risk.

Choose who bears the remainder

Flooring minted shares and ceiling required payment can protect different invariants. The correct direction depends on the operation rather than one universal rule.

Test repeated small actions

A tiny one-step rounding difference can accumulate when an action is repeated or split. Check sequences, not only a large representative amount.

Document the invariant

Explain the quantity that must remain conserved or bounded. Tests should assert that property across deposit, withdrawal and conversion boundaries.

Reason about the scenario

integer floor: 5 / 2 = 2
integer ceiling for the same positive ratio: 3

Choose deliberately and test who receives the remainder.

Pseudocode or a simplified scenario for local reasoning. This is not a production contract or a completed security review.

QUICK CHECK · NO PRESSURE

Is one rounding direction universally correct for every operation?

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.