PHP comparing equal numbers returns an incorrect result

Viewed 35

I'm baffled by this. I have 2 variables which both contain the same value, but when I compare them to see if one of them is lower than the other, it returns true when it shouldn't

echo '
',$orderTotal,'
',$invTotal,'
';
var_dump(
   $orderTotal,
   $invTotal,
   (float)$orderTotal,
   (float)$invTotal,
   number_format($invTotal,4),
   number_format($orderTotal,4),
   (float)$orderTotal<(float)$invTotal,
   (float)$invTotal<(float)$orderTotal,
   (float)$invTotal==(float)$orderTotal,
   (float)$invTotal===(float)$orderTotal
);

As you can see I've tried all sorts of ways to see the content of the variables, because I thought maybe they had some decimal values that I couldn't see, but the output is this:

1.11
1.11
string(4) "1.11"
float(1.11)
float(1.11)
float(1.11)
string(6) "1.1100"
string(6) "1.1100"
bool(false)
bool(true)
bool(false)
bool(false)

What's wrong here is that (float)$invTotal<(float)$orderTotal returns true and (float)$invTotal==(float)$orderTotal returns false, and I don't understand why!

Is this just something really dumb that I'm not seeing?


EDIT

So the answer apparently is to compare the values by using round($invTotal,2)<round($orderTotal,2) to eliminate any extra decimal values that apparently exist but are never displayed anywhere..

0 Answers
Related