Unit mistakes can hide inside otherwise simple arithmetic. A contract may receive one token's smallest units, combine them with a scaled rate and present the result using another token's display precision. The system needs an explicit account of those units and of every rounding decision. Start with the full path from user input to stored value and back to the displayed result.
Annotate each quantity with its unit
List amounts, rates, percentages, timestamps and counters used in consequential calculations. Record their scale and meaning, not merely their integer type. OpenZeppelin's ERC-20 guidance explains that decimal presentation is distinct from integer accounting. Apply that distinction throughout the product. A variable called amount is insufficient when several assets are involved. Use naming and documentation that let a reviewer follow conversions without guessing. Include off-chain calculations and transaction builders, because a correct contract can still receive a value scaled incorrectly by the interface.
Specify the order of operations
Write the intended calculation in a form a product owner and engineer can both inspect. Determine where division occurs and which precision is retained between steps. Integer arithmetic can produce different results depending on operation order, so small-value cases deserve attention. Review overflow and range behaviour under the chosen compiler and implementation rather than relying on a general assumption. The specification should explain why the chosen result is appropriate for the product, especially when a conversion or fee affects what a user receives.
Choose rounding as a policy
Decide whether each calculation rounds up, down or follows another supported rule. Consider who benefits from the remainder and whether repeated small operations change the outcome compared with one large operation. A tiny difference can matter when an action is repeated many times. Document minimum amounts and unusable remainders. Avoid displaying a rounded estimate as an exact execution result. The user should see the actual amount used for confirmation, with enough precision to understand a meaningful difference without being overwhelmed by irrelevant digits.
Test boundaries and composition
Include zero, one smallest unit, exact multiples, values around a rounding threshold and the largest supported amount. Test assets with different decimal conventions and unsupported configurations. Compare repeated partial operations with an equivalent combined operation where the product expects consistency. Use independent expected results rather than duplicating the implementation's formula verbatim in the test. A calculation can be internally consistent and still encode the wrong policy. The tests should establish that the chosen arithmetic matches the documented meaning of the exchange or entitlement.
Keep presentation and accounting aligned
Review formatting, localisation and input parsing across the interface, backend and contract. Reject ambiguous or unsupported input clearly. Preserve exact values in operational records even when the interface uses a friendlier display. When a calculation changes, version the interpretation needed for historical transactions. A consistent unit model makes the system easier to audit and support because every amount has a traceable meaning. It also prevents small rounding surprises from becoming a recurring source of distrust in a product whose main promise is precise digital accounting.
The key decisions.
- Decide whether each calculation rounds up, down or follows another supported rule.
- Include zero, one smallest unit, exact multiples, values around a rounding threshold and the largest supported amount.
- Review formatting, localisation and input parsing across the interface, backend and contract.
Sources & further reading
Primary references for the concepts discussed. Check the applicable documentation when planning implementation.







