0

I have a problem with an if statement. The program doesn't go inside the if when it seems that the condition is true.

I tried to change the operator >= to == but it still doesn't work. It works with <= but I need >= and I also can't explain to myself why it's not working.

<?php

$balance = 0.03;      // input 0.03 

if($balance >= 0.02)
{
    $returned = ((floor($balance / 0.02)) * 0.02); // 0.02
    $balance = $balance - $returned;               // 0.03 - 0.02 = 0.01 left
}

echo var_dump($balance) . PHP_EOL;

if($balance >= 0.01)                // why doesnt go in ?
{
    echo "Print Smt ....";
}

Why is the second if not true? Isn't 0.01 equal to 0.01?

Boann
  • 48,794
  • 16
  • 117
  • 146
JustInCase
  • 41
  • 3
  • 1
    When working with currency, use bcmath https://www.php.net/manual/en/book.bc.php – Tim Morton Jun 22 '19 at 17:10
  • 1
    on line 13 $balance is actually `0.0099999999999999984734433411404097569175064563751220703125` which is less than 0.01, but when var_dump wants to show it, var_dump truncates it to something more readable: 0.01 - use `var_dump(number_format($balance,100))` to see the full number. – hanshenrik Jun 22 '19 at 18:29

1 Answers1

0

Because $balance is reassigned value -1.47 which is less than 0.01.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • 1
    actually, by line 13 it has the value `0.0099999999999999984734433411404097569175064563751220703125` - use `var_dump(number_format($number,100));` to see the full number – hanshenrik Jun 22 '19 at 18:33