gevensen Posted February 9, 2012 Share Posted February 9, 2012 I am comparing 2 decimal numbers I am pulling from a mysql file for ex $row[0]==$row1[0] every once in a while i get a bad positive when I do if($row[0]==$row1[0]) when they are exactly the same? One query is a SELECT amount FROM table the other is a select SUM(amount) FROM table2 any ideas why? ive tried making them both abs() when I print the out they are = Quote Link to comment https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/ Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 A couple good answers here for why this occurs. Quote Link to comment https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/#findComment-1316317 Share on other sites More sharing options...
smerny Posted February 9, 2012 Share Posted February 9, 2012 and how to work around it: http://www.php.net/manual/en/language.types.float.php $a and $b are equal to 5 digits of precision. <?php $a = 1.23456789; $b = 1.23456780; $epsilon = 0.00001; if(abs($a-$b) < $epsilon) { echo "true"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/#findComment-1316332 Share on other sites More sharing options...
gevensen Posted February 10, 2012 Author Share Posted February 10, 2012 ok yesterday i wrapped the decimal values like this, numbers are an example <?php $decimal1=2.50; $decimal2=2.50; if(strval($decimal1)!=strval($decimal2))( blah blah blah } ?> that worked if i am understanding the eplison thing i could also <?php $decimal1=2.50; $decimal2=2.50; $epsilon = 0.00001; if(abs($decimal1-$decimal2) < $epsilon) { echo "true"; } ?> correct?? Quote Link to comment https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/#findComment-1316528 Share on other sites More sharing options...
gevensen Posted February 10, 2012 Author Share Posted February 10, 2012 sorry dumb question i tried it and it works thanks! Quote Link to comment https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/#findComment-1316531 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.