0

i want to make a simple calculation: 21.60 / 72 and i know the result is 0.3 but it seems the code isnt giving the expected result and echos "Not ok", can any of you figure this out?

if (21.60 / 72 == 0.3) {
        echo "Ok";
    }
    else {
        echo "Not ok";
    }
Henrique
  • 43
  • 1
  • 6
  • 1
    The duplication doesn't give a good solution on the issue, but you can fix it by using [number format](http://php.net/manual/en/function.number-format.php) – IsThisJavascript Sep 12 '17 at 14:33
  • 1
    Welcome to the world of floats in PHP. If you `var_dump()` the result of `21.60 / 72` you'll see that you're probably not getting exactly `0.3` as a result. Try rounding the result before comparing it. – rickdenhaan Sep 12 '17 at 14:34
  • Have you tried echo what was the actual result of this calculation instead of just echo sting ok or not ok? – Houy Narun Sep 12 '17 at 14:38
  • Thats realy strange. My result is 0.3 with var_dump and echo but its not possible to get a true result only if i add something to get a higher float like 1.3 – A. Blub Sep 12 '17 at 14:40
  • as @rickdenhaan point out the result of `21.60 / 72` was returning not exactly result `0.3` as you expected. Since it's kind of `floating point` type, via `var_dump()` function, you should have a way to round it to get exactly as you expected. Try this should work for you ``. Thanks – Houy Narun Sep 12 '17 at 15:08

0 Answers0