mjohnson025 Posted February 27, 2008 Share Posted February 27, 2008 Hi all, I'm having a very difficult time figuring out how to make the program determine the proper bills for this project. I've got it showing me the change, but i need to make is seperate it into 50, 20, 10, etc.. Can anyone give me an idea of where to start?? Thanks, Mike Here is what i have so far. (So nobody thinks i'm being lazy.) :-) <?php if (isset($_GET['cost']) && isset($_GET['paid'])){ if (is_numeric($_GET['cost']) && is_numeric($_GET['paid'])){ $cost = $_GET['cost']; $paid = $_GET['paid']; $formattedCost = number_format($cost,2); $formattedPaid = number_format($paid,2); echo "<p>\$" . $formattedCost . "\n</p>"; echo "<p>\$" . $formattedPaid . "\n</p>"; $change = $formattedPaid - $formattedCost; echo "<p>\$" . $change . "\n</p>"; } else echo "<p>You must enter a numeric value!</p>"; } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 27, 2008 Share Posted February 27, 2008 this is just an algorithm processing downward from the highest bill amount, right up until there's no change left to give: <?php $change_rendered = array(); $bills = array(50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01); foreach($bills AS $bill_amt) { // how many bills of this type we need $num_bills = floor($change / $bill_amt); // add it to the change rendered array $change_rendered = array_merge($change_rendered, array($bill_amt => $num_bills)); // subtract the amount from the change $change -= $num_bills*$bill_amt; // exit the loop if the change is now at 0, because we've rendered all we need to if ($change == 0) break; } ?> note that i haven't tested it, but it SHOULD work. Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted February 27, 2008 Author Share Posted February 27, 2008 reading it it looks like it should work, but something's not quite right. I don't understand the reasoning for the array_merge. I also am not sure which variable is returning the information I need to print to the screen (50's = 1, 20's = 0, 10's = 1, etc..) I really appreciate your help! Mike Quote Link to comment Share on other sites More sharing options...
DarkerAngel Posted February 27, 2008 Share Posted February 27, 2008 $change_rendered is the return Quote Link to comment Share on other sites More sharing options...
Barand Posted February 28, 2008 Share Posted February 28, 2008 you could replace $change_rendered = array_merge($change_rendered, array($bill_amt => $num_bills)); with $change_rendered[$bill_amt] = $num_bills; if that makes it clearer Quote Link to comment Share on other sites More sharing options...
moon 111 Posted February 29, 2008 Share Posted February 29, 2008 US uses $1,000 bills (and beyond I think). Just thought I should mention it. The array only has values up to $50. Quote Link to comment Share on other sites More sharing options...
akitchin Posted February 29, 2008 Share Posted February 29, 2008 it was implied that the OP could adjust the array to contain all necessary bill or coin amounts available to a server. if you use print_r($change_rendered), you'll get an idea of the structure of the final array and therefore you should be able to work out how to present it. as for the array_merge(), i used it to avoid a notice about an undefined key; i don't know if this is avoided when one uses numerical keys (as opposed to string indexes), but barand's solution should make the line clearer as mentioned. Quote Link to comment Share on other sites More sharing options...
mjohnson025 Posted March 2, 2008 Author Share Posted March 2, 2008 thanks for everyone's help! I really appreciate it! Mike Quote Link to comment 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.