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 = 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. 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"; } ?> 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?? 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! 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
Archived
This topic is now archived and is closed to further replies.