Most languages seem to use bankers' rounding.
This means midway values are rounded to the nearest even value.
- So round(2.5) becomes 2.
-
round(3.55,1) becomes 3.6
To force a mid value always up, the trick is to add a constant EPSILON.
This is an insignificant small value that won't throw out the sums but enough to trip rounding the right way.
- round((qty * price) + EPSILON,2)
The same technique is used when comparing floating point numbers
2.5 == 5/2 may not return true, so instead compare the result to EPSILON.
- if((2.5 - 5/2)) < EPSILON)
So EPSILON is your tolerance. It should be smaller than the precision you will be displaying but slightly larger than the precision you are calculating.
So if calculating in three decimal places, but displayng the result in two, EPSILON would be 0.001.