0

I have some code here that is acting really weird on my website.

if($balance != $expectedBalance){
echo "balance: ".$balance." - Type:".gettype($balance);
echo "<br>";
echo "expected: ".$expectedBalance." - Type:".gettype($expectedBalance);

But the output of this code is:

balance: 69.95 - Type:double

expected: 69.95 - Type:double

How is that, its really making me go crazy when developing my website. And how do I make it so the if statement actually only runs if the are diffrent?

Community
  • 1
  • 1
  • 2
    Try `var_dump($balance)` and `var_dump($expectedBalance)` to see exactly what is in those values and the size. – aynber May 08 '17 at 19:07
  • 3
    Floating point numbers are not exact. You can have two floating point numbers that print the same but have some differences in low-order bits. – Barmar May 08 '17 at 19:08
  • Could be floating point inaccuracies, but since there are only 2 decimal places I doubt it. Perhaps instead of just echo'ing the value you could do a var_dump on both $balance and $expectedBalance. – Jonathan Kuhn May 08 '17 at 19:08
  • It's best not to use floating point for money for this reason. – Barmar May 08 '17 at 19:10
  • @aynber when using var_dump i get "float(69.95) " on both variables...? – Anton Kihlström May 08 '17 at 19:11
  • @JonathanKuhn when using var_dump i get "float(69.95) " on both variables...? – Anton Kihlström May 08 '17 at 19:13

0 Answers0