PHP price to int is losing a cent<>
This question already has an answer here:
- Is floating point math broken? 24 answers
FuzzyTree's answer explained the problem with floating point arithmetic. You can fix it by using round before intval.
<?php $val = 568.3 * 100; echo $val; echo "\n"; echo (float) ($val); echo "\n"; echo (int) (round($val)); echo "\n"; echo intval(round($val)); echo "\n"; ?>
56830 56830 56830 56830
$val is float with a value of something like 56829.999999... and it gets rounded down when converting to an int.
From the manual
From floating point numbers ¶
When converting from float to integer, the number will be rounded towards zero.
If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms other than Windows), the result is undefined, since the float doesn't have enough precision to give an exact integer result. No warning, not even a notice will be issued when this happens!