Forums / Bug Reports / double rounding

double rounding

Locked

Started by tbee on 17 Mar 2009, 3:17 PM · Views: 1,757

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
17 Mar 2009, 3:17 PM
#1
tbee avatar

tbee

New Zenner

Join Date:
Mar 2009
Posts:
15
Plugin Contributions:
0

double rounding

I had a problem with a price being rounded incorrectly. The webshop I have should have prices ending on 0 or 5 (so 1.00, 1.05, 1.10, etc).

The tax rate is 19%, so if I have a price without taxes of 37.605, than to me the price including taxes is:

37.605 * 1.19 = 44.74995 => 44.75

However zen-cart 1.3.8 comes with 44.74 because it does some strange rounding. To zen-car the price including taxes is:

zen_round(37.605,2) + zen_round(37.605 * 0.19,2) =
36.6 + 7.14 = 44.74

The I do not understand when 37.605 is rounded to 37.6. The numbert of decimals (scale) is set to 2, so it should have rounded to 37.61.

If I execute "round(37.605,2)" directly, I still get 37.6... This is wierd, PHP per default does "round half up".

17 Mar 2009, 3:32 PM
#2
tbee avatar

tbee

New Zenner

Join Date:
Mar 2009
Posts:
15
Plugin Contributions:
0

Re: double rounding

I have replaced the round($number, $precision) with

$t = ($number * pow(10,$precision)) + 0.5;
$t = (int)("" . $t);
$t = $t / pow(10,$precision);

And now the outcome is correct.

18 Mar 2009, 7:14 PM
#3
tbee avatar

tbee

New Zenner

Join Date:
Mar 2009
Posts:
15
Plugin Contributions:
0

Re: double rounding

Appearantly this is semicorrect behavior

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23201889.html

The number simply isn't accurate enough for this specific number.

However, I would then move to suggest that zencart should not use PHP's round function and use one of the alternatives suggested in the link. (Or my replacement hack.)

18 Mar 2009, 7:16 PM
#4
tbee avatar

tbee

New Zenner

Join Date:
Mar 2009
Posts:
15
Plugin Contributions:
0

Re: double rounding

http://www.zen-cart.com/forum/showthread.php?p=702001

If a products has price 37.605 then this is rounded in 2 decimals to 37.60 instead of 37.61. PHP's round function does not handle floats very well.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23201889.html

Zencart should use a better rounding function as explained in the link above.