I have the following code
$amount1 = 7299;
$amount2 = 72.9875;
$amount2_in_cents = round($amount2, 2) * 100;
if ($amount1 != $amount2_in_cents) {
echo "Amount $amount1 != $amount2_in_cents
";
var_dump($amount1);
var_dump($amount2_in_cents);
} else {
echo "Amounts matched";
}
and this is the output
Amount 7299 != 7299
int(7299)
float(7299)
Now I realise that floats and int are different, but given the rounding i would have expected the two values to match. And I have solved it by casting to int.
So my question is why does this comparison not work as i would have expected (both values matching)?
See Question&Answers more detail:os