In this track: Digital value and units
Add and subtract exact ETH amounts
Convert to a common whole unit before combining amounts.
Adding centimetres to metres without conversion gives the wrong total. Using one common unit keeps the calculation clear.
1 ETH = 1000000000 Gwei = 1000000000000000000 Wei.
Choose a common scale
Suppose Maya has 0.1 ETH and receives 0.2 ETH. Parse each input into Wei: 100000000000000000 and 200000000000000000. Add these exact integers, then format 300000000000000000 Wei as 0.3 ETH.
Subtraction needs a balance check
If a calculation subtracts more than the available amount, decide how the application should handle it. Native balances cannot simply become negative. An unsigned contract calculation can revert on underflow under ordinary checked arithmetic.
Account for fees separately
An ETH payment may require both its value and a fee budget. Having exactly the payment amount is not necessarily enough to send it. A fee estimate is not a guarantee, so an interface must explain what its spendable-balance calculation assumes.
Different assets do not add directly
One ETH plus one token is not two ETH. Even two ERC-20 balances with identical symbols may belong to different contracts. Keep network, asset identity and raw scale attached to every amount you combine.
What is the safe first step when adding displayed ETH amounts?
Use a common exact unit for arithmetic, then format the result for display.
Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.