Document Type | Technical Information
Field | App Development
Applicable Product Versions | Tibero 6, Tibero 7
Document Number | TDETS020
Issue
You can observe the phenomenon where mathematically identical formulas return different results when applying ROUND as follows.
SQL> select round(8003/60*30,0) from dual ;
ROUND(8003/60*30,0)
-------------------
4001
SQL> select round(8003*30/60,0) from dual ;
ROUND(8003*30/60,0)
-------------------
4002
Cause
The final ROUND result value differs depending on the order of operations.
| Formula | Intermediate Result | Final Value | ROUND |
|---|---|---|---|
8003/60*30 | 133.38333โฆ333 | 4001.49999...999 | 4001 |
8003*30/60 | 24090 | 4001.5 | 4002 |
Solutions
Since calculation results for sensitive data (amounts, quantities) can differ, it is recommended to apply the following workarounds and rules.
- Perform multiplication first, then division as a standard coding rule, leading with integer multiplication before inducing division.
- Explicitly apply
ROUND/TRUNCto intermediate results to strictly control decimal places. Preserve decimal points as much as possible to induce the desired rounding after rounding.
SQL> select round(round(8003/60*30,20), 0) from dual ; ROUND(ROUND(8003/60*30,20),0) ----------------------------- 4002 SQL> select round(8003*30/60,0) from dual ; ROUND(8003*30/60,0) ------------------- 4002