1
$package_with_fee = $packages_lot_qty * 1.1;

$total_sum = floatval($total_sum);
$package_with_fee = floatval($package_with_fee);


echo "Total sum is $total_sum vs Package with fee cost $package_with_fee";
if( $total_sum != $package_with_fee)
{

echo "ERROR";

}

When I run the code, this is what I get

Total sum is 3300 vs Package with fee cost 3300ERROR

I not sure why its wrong, both php variables are 3300, but its just doesn't match and throw error. what could went wrong?

  • 1
    Take a look at this page and you will understand what the issue is: http://floating-point-gui.de/ – Jite Dec 12 '16 at 19:38
  • Do `echo sprintf("%.20f", $variable);` for both variable names and then read: http://stackoverflow.com/q/588004 – Rizier123 Dec 12 '16 at 19:39
  • floating numbers are very approximatives, it's very complicated for a computer to be very precise while representing floating numbers and calculating them – Alexandre Dec 12 '16 at 19:41
  • http://stackoverflow.com/questions/8364189/comparing-floats-same-number-but-does-not-equal It's a float issue, you need to format using sprintf as Rizier mentions. Just change to intval instead of floatval just to test, and you'll see that it works fine and that the problem is with the use of float. – Andrew Larsen Dec 12 '16 at 19:47
  • @Rizier123 sprintf works. – Robert Pilera Dec 12 '16 at 19:49

1 Answers1

-1

Do var_dump to both variables and see difference. There is something minor differences like format or some any other . You can also have look about floatval http://php.net/manual/en/function.floatval.php

Ashish Tiwari
  • 1,919
  • 1
  • 14
  • 26
  • 1
    there is no visible difference when doing var_dump on the 2 variables – Andrew Larsen Dec 12 '16 at 19:58
  • Hi , this is the issue of floating point precision as explained here http://php.net/manual/en/language.types.float.php . if you want to compare than please convert your value to integer first and than convert into float OR you can refer different method also which explained in above links. – Ashish Tiwari Dec 13 '16 at 04:51