Accurate Calculation

A

Anonymous

Guest
Hi guys,

I've stumbled across a (probably common) problem when calculating a summary of prices. Here's the example:

2 items @ 4.25
- 10% discount per item (price-(price*0.1)) = 3.825 (php is rounding this to 3.83)

When I calculate the a subtotal it returns 7.65 (which is correct) but this does not tally with the items listed at 3.83 x 2.

The prices are being extracted from MySQL in a DECIMAL(10,2) datatype. I have tried to alter this to DECIMAL(10,4), but no difference.

I think I need to somehow calcuate the prices with 4 decimal places? But I'm not sure how to do it.

Has anyone overcome this sort of thing before?
Many thanks in advance.
 
Please show some of your code. I can't quite make sense of your question.
 
If I understand the problem correctly, the issue is a rounding error. More specifically, and error on when to round. If you get a total and then subtract 10%, it will be correct. If you take off 10% for each item, and then total the 90% prices, the total may be off due to rounding at the wrong time.

The point is....
90% of the total can be different from the total of the all the 90% prices.
 
Back
Top