I had to learn this the hard way. It seems in php 27.64 is just not quite 27.64 , but something close to it ( that's what some people say here anyway: http://www.php.net/ternary ) .
And that's why the following code wouldn't echo the text "dude" as you might think it should:
span style="color: #ff0000;">"amountdue: $amountdue, amount: $amount\n""dude\n""but still no match\n"
Run this script and you should see:
amountdue: 27.64, amount: 27.64 but still no match
That's the dumbest thing I've ever seen. A similar code written in C gives the good results. Isn't php written in C? They should have copied C's good behavior.
If conversion from float to string gives the right results, why not use the same results when comparing float values?
So if you're having this problem it seems this will work:
The fact that php can't compare float values reliably and that you have to resort to this to make it work just makes php look bad.
“So never trust floating number results to the last digit, and never compare floating point numbers for equality.” (Source: http://us3.php.net/float )
sad but true.
Well it’s not just sad it’s stuppid. I understand this might not work for high precision, numbers with a lot of decimals but for stuff like 2.64 compared to 2.64 it’s just stupid. If the comparison works in C it should work just as well in php.
Agreed
Same problem here it’s ludicrous
A !=A in PHP
let’s convert strings instead
Just multiply them and then compare int
or explode and compare int one at a time
it’s simpler to just cast to string
Define a precision value and then compare:
Example 1: if(round($a,4) == round($b,4)
Example 2: if(abs($a-$b) < 0.0001)
See ya!