Forums / Bug Reports / [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

[OPEN CHANGE-358]Products with specials price can cause rounding error in order total

Started by sam_m on 17 Oct 2011, 1:32 PM · Views: 3,291

Results 1 to 12 of 12
17 Oct 2011, 1:32 PM
#1
sam_m avatar

sam_m

New Zenner

Join Date:
Nov 2007
Posts:
71
Plugin Contributions:
0

[OPEN CHANGE-358]Products with specials price can cause rounding error in order total

Hello,

Im having an annoying issue with calculating tax...

Basically if I use this as an example...

Shipping: €3.94
Subtotal: €55.37
Tax 18%: €10.68

Total: €69.99

The Shipping and subtotal needs the 18% added which to me works out at 10.67

Now how come the shop is rounding it up to 10.68, On the calculator is shows... 10.6758 so im guessing this is the reason why its rounding up.

Is there a way to stop this, and ONLY read the 2 digits after the decimal point without rounding it up

Thank you
Sam

17 Oct 2011, 3:29 PM
#2
Kim avatar

Kim

Obaa-san

Join Date:
Jun 2003
Location:
West Coast, North America
Posts:
26,718
Plugin Contributions:
0

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

What version of Zen Cart? How many items were in the cart and what are their prices?

Please do not PM for support issues: a private solution doesn't benefit the community.

Be careful with unsolicited advice via email or PM - Make sure the person you are talking to is a reliable source.

23 Feb 2012, 1:19 AM
#3
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

Same problem in a 1.3.9h install.

See image at frnt dot org / tax-rounding dot gif

The issue must have existed for some time. It is only now that a customer pointed out the error.

I did a comparison of the relevant files (original 1.3.9h install files vs live install) and cannot find a flaw.

Frank

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

23 Feb 2012, 1:28 AM
#4
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

I should add that this product is part of a sale.

Original price $28.95 (inc tax)
discount 5% = $1.45
sale price $27.50 (inc tax)

see product page

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

23 Feb 2012, 4:30 AM
#5
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

After digging through the DB I established that both (products_price and final_price) are stored with 4 decimals, in my example 25.0023 (excl tax) derived from 27.5025 (inc 10% tax) and rounded from 25.0022727272

original product price $28.95 (inc tax)
discount 5% = $1.45 which in the background is calculated as 1.4475
sale price $27.50 (inc tax) visible to customer but calculates as 27.5025

this is the result of 28.95 - 1.4475 = 27.5025

on the invoice this is displayed at Price (inc) = $27.50

now the order contains say 7 items, the customer expects $27.50 x 7 = $192.50 but gets charged $27.5025 x 7 = $192.5175 which rounds to $192.52

It seems that these calculations are using a function that always uses 4 decimals. If that function (which I still need to find) is changed to 2 decimals then the problem IMHO should be solved ... ?? :lookaroun

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

23 Feb 2012, 4:59 AM
#6
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

Found it!

In includes/functions/functions_prices.php around lines 94-107 there are 5 instances of

return number_format($blah_blah, 4, '.', '');

set to 4 decimals. Changed these to 2 and replicated the order from my previous example.

Bingo! :smile:

As the customer I was charged exactly $192.50 and not $192.52

In the DB table orders_products both (products_price and final_price) are now found as 25.0000 and not as 25.0023 (excl tax)

The calculation is now performed without fractions of cents which seems to have caused the error.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

23 Feb 2012, 5:38 AM
#7
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

The following applies to v 1.3.9x and 1.5, not sure about earlier versions:

In includes/functions/functions_prices.php around lines 94-107 there are 5 instances of

return number_format($blah_blah, 4, '.', '');

set to 4 decimals.

Depending on the specials price of a given product this can cause fractions of cents which add up to one or several cents being added to the order total (products price x qty)

Changing the decimals to 2 eliminates the error.

For further explanation see posts #3-6 above.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

10 Jul 2012, 5:45 PM
#8
simpledomaining avatar

simpledomaining

New Zenner

Join Date:
May 2011
Posts:
89
Plugin Contributions:
0

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

i also have this problem with my 1.3.9h version. we did fixed up rounding from items on special but this happened today: customer purchased an item on sale and paid local tax. when this two numbers add up its 1 penny more from what it should be. as a result we cant import this order into our quickbooks. But when person purchases 2 items on sale without paying local tax this rounding problem is not happening, all adds up smooth.

10 Oct 2012, 9:10 PM
#9
coins2012 avatar

coins2012

New Zenner

Join Date:
Jan 2012
Posts:
89
Plugin Contributions:
0

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

I am curious if I follow your fix if it would solve my problem, below is the image of an order, note that Total(ex)=81.57 but Total(inc) is 81.56 there is a penny difference and have to fix this manually in my accounting software and it's pain to go through many orders everyday.

[Attachment no longer available]

10 Oct 2012, 9:48 PM
#10
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

Coins2012:

I am curious if I follow your fix if it would solve my problem, below is the image of an order, note that Total(ex)=81.57 but Total(inc) is 81.56 there is a penny difference and have to fix this manually in my accounting software and it's pain to go through many orders everyday.

[Attachment no longer available]

Backup your includes/functions/functions_prices.php file and apply the fix. If it works for you then all is good. If not then you can always restore the backup.

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

17 Oct 2012, 6:23 PM
#11
coins2012 avatar

coins2012

New Zenner

Join Date:
Jan 2012
Posts:
89
Plugin Contributions:
0

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

frank18:

Backup your includes/functions/functions_prices.php file and apply the fix. If it works for you then all is good. If not then you can always restore the backup.

It did not fix the problem, looking at the Database for Product Table the prices has 4 decimal points and the issue arises from there. Since I have calculated product prices based on precious metal prices it creates this issue, if there was a setting in the database to make the numbers rounded to 2 decimal points that would perhaps solves the problem.

10 x 2013 Canadian Silver Antelope Coin Wildlife Series With Air-Tite (1-ANTELOPE-AIR-TITE-2013) $399.57
---------
Sub-Total: $399.60
Per Item (Best Way): $0.00
Total: $399.60

25 Mar 2014, 8:48 AM
#12
frank18 avatar

frank18

Deceased

Join Date:
Nov 2007
Location:
Sunny Coast, Australia
Posts:
3,427
Plugin Contributions:
2

Re: [OPEN CHANGE-358]Products with specials price can cause rounding error in order total

I know this is an old thread - BUT:

On one of my client's stores (v. 1.5.1 - just upgraded) the same issue is still persistent.

I have applied my fix as per post #7 of this thread and changed the decimals from 4 to 2.

Once again: this has fixed the rounding error on a 1.5.1 install.

Guess this still needs looking into for future zc versions.

Cheers / Frank

NOTE: We are sorry that Frank is no longer with us.
We are grateful for all his contributions to the Zen Cart community.