Jump to content

Problem with decimal numbers


Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...