Jump to content

seperate bills for $change


mjohnson025

Recommended Posts

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>";
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.