So, I got this:
$a = "149.45";
var_dump($a);
gives
string(6) "149.45"
now
$b = $a * 100;
var_dump($b);
so far so good:
float(14945)
and now
$i = (int)$b;
var_dump($i);
What the …?
int(14944)
Tested on PHP 5.2.17 (on CentOS) and PHP 5.3.2-1ubuntu4.9 (On ubuntu).
Can anyone tell me if this is a known bug?
Can anyone test it on a newer PHP version?
EDIT
OK, I got it, float binary representation aproximation error. For my code I already used round().
But why then
$a = 149.45;
$b = 100 * $a;
var_dump($b)
printf('%.10f', $b);
gives
float(14945)
14945.0000000000
? Why that string is not parsed just as 149.45 is parsed?
Illustration of what @Jon said:
result
“14945” is just a string representation of the above number, not that number itself.