math - strange behavior when calculating numbers in PHP -
when run following php script calculating discount of 30% 11.5. i'll strange result. expect condition false when testing calculated result. instead true.
whats wrong php in case?
<?php $_discount = 30; $_price = 11.5; $_qty = 1; echo $_result = ((1-$_discount / 100) * $_price); // result 8.05 echo $_result; // prints 8.05; echo gettype($_result); // prints double echo $_result !== 8.05; // returns 1 instead of 0 ?>
try use this:
<?php $_discount = 30; $_price = 11.5; $_qty = 1; echo $_result = ((1-$_discount / 100) * $_price); // result 8.05 echo $_result; // prints 8.05; echo gettype($_result); // prints double echo (double)$_result !== 8.05; ?>
Comments
Post a Comment