0

I have got following code in php:

$canPrice = 4.98;
$insertedMoney = 5;
$remainingRest = $insertedMoney - $canPrice;
echo $remainingRest;
if( $remainingRest == 0.02 )
    echo "equal";
else
    echo "not equal".$remainingRest;

nothing more, it should print equal but somehow it's printing not equal, through I see, $remainingRest is equal 0.02.

Thanks for help in advance

tookie009
  • 186
  • 1
  • 14
  • 1
    check this topic to answer your question https://stackoverflow.com/questions/5271058/odd-behavior-comparing-doubles-two-php-double-values-arent-equivalent – Majid Ahmadi Apr 14 '20 at 11:10
  • 1
    Does this answer your question? [Odd behavior comparing doubles, two PHP double values aren't equivalent](https://stackoverflow.com/questions/5271058/odd-behavior-comparing-doubles-two-php-double-values-arent-equivalent) – Pierre Apr 14 '20 at 11:14

1 Answers1

1

Returns false because float values ​​don't equal exactly when equalize.

You can try to equalize;

<?php
   $canPrice = 4.98;
   $insertedMoney = 5;
   $remainingRest = $insertedMoney - $canPrice;

   if(round($remainingRest,2) == round(0.02,2))
        echo "equal";
   else
        echo "not equal".$remainingRest;
?>
Umutcan
  • 65
  • 6