taterman Posted April 6, 2011 Share Posted April 6, 2011 Hello,I'm using a while loop that takes a 2 digit decimal number ($tmp) and subtracts different coin values from it until $tmp is equal to zero. For some reason it never reaches zero unless the $tmp value is either in multiples of 25 or 10. Below is the code I basically use and the output I receive.What am I missing so when 0.01 - 0.01 = 0 or 0.05 - 0.05 = 0? Thanks for your time. $tmp = .73;while($tmp != 0){ if($tmp >= .25){ echo $tmp."-.25<br />"; $tmp -= (.25); }elseif($tmp >= .10){ echo $tmp."-.10<br />"; $tmp -= (.10); }elseif($tmp >= .05){ echo $tmp."-.05<br />"; $tmp -= (.05); }else{ echo $tmp."-.01<br />"; $tmp -= (.01); }} Output I get:0.73-.250.48-.250.23-.100.13-.100.03-.010.02-.010.01-.01-3.1225022567583E-17-.01-0.01-.01-0.02-.01-0.03-.01-0.04-.01..... Link to comment Share on other sites More sharing options...
jeffman Posted April 6, 2011 Share Posted April 6, 2011 Life is not what it seems. See the WARNING here: http://www.php.net/manual/en/language.types.float.php Link to comment Share on other sites More sharing options...
taterman Posted April 6, 2011 Author Share Posted April 6, 2011 Thanks for pointing me in the right direction! I'll read up on this now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.