just started taking php need help with a code we have to break change down by how many quarters,dimes,nickels and pennies you will give back if say something cost 6.65 and you where given a ten.
I know how to solve it has far as making the loops to subtract the amount but having trouble in putting it in php code.
how about this:
[php]
Item cost £' . number_format($itemCost, 2) . '
'; echo 'You pay £' . number_format($youPay, 2) . '
'; echo 'Pennies in change = ' . number_format($pennies, 2) . '
'; echo 'Nickels in change = ' . number_format($nickels, 2) . '
'; echo 'Dimes in change = ' . number_format($dimes, 2) . '
'; echo 'Quarters in change = ' . number_format($quarters, 2) . '
'; [/php] will output: [sup] Item cost £6.65 You pay £10.00 Pennies in change = 3.35 Nickels in change = 0.67 Dimes in change = 0.34 Quarters in change = 0.13[/sup] or swap the last bit for this: [php] echo 'Pennies in change = ' . str_replace('.', '', str_replace('0', '', number_format($pennies, 2))) . '
'; echo 'Nickels in change = ' . str_replace('.', '', str_replace('0', '', number_format($nickels, 2))) . '
'; echo 'Dimes in change = ' . str_replace('.', '', str_replace('0', '', number_format($dimes, 2))) . '
'; echo 'Quarters in change = ' . str_replace('.', '', str_replace('0', '', number_format($quarters, 2))) . '
'; ?>[/php]
will output:
[sup]
Pennies in change = 335
Nickels in change = 67
Dimes in change = 34
Quarters in change = 13
[/sup]
I hope this is what you were looking for, or it has given you an idea how to accomplish your task.
Hi there,
This really interested me, so I’ve gone ahead and written a script that allows you to easily change the currency.
<?php
$currency = "£";//Set currency sign
$before = true;//Does the sign come before the numbers
//Set array of types of money and their value
$money = array(
"1 pound" => 1
,"50 pence" => 0.5
,"20 pence" => 0.2
,"10 pence" => 0.1
,"5 pence" => 0.05
,"2 pence" => 0.02
,"1 penny" => 0.01
);
if(isset($_POST['formsub']))
{
$cost = (float)trim(strip_tags($_POST['cost']));
$given = (float)trim(strip_tags($_POST['given']));
if($given > $cost)
{
$remaining = $given-$cost;//Work out the change
$i = 0;
foreach($money as $name => $value)
{
if($remaining >= 0)
{
if($remaining/$value >= 1)
{
$loc = strpos($remaining/$value,".") || strlen($remaining/$value);//Find where the remainder starts
$change[$name][0] = (float)str_replace(".","",substr($remaining/$value,0,$loc+1));//Get the core (floored) integer for divisions
$remaining = number_format($remaining-($change[$name][0] * $value),2);//Work out the remaining money to be dealt with
}
$i++;
}
else
{
break;
}
}
}
else
{
echo "Money given was not greater than the actual cost. Please try again.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
</head>
<body>
<?php
if(isset($change))
{
//Add the currency sign wherever chosen above
$cost2 = ($before) ? $currency.$cost : $cost.$currency;
$given2 = ($before) ? $currency.$given : $given.$currency;
$changedue = ($before) ? $currency.($given-$cost) : ($given-$cost).$currency;
//Display the figures that we have been working with
echo "<strong>Actual Cost:</strong> ".$cost2;
echo "<br />";
echo "<strong>Money Given:</strong> ".$given2;
echo "<br />";
echo "<strong>Change Due:</strong> ".$changedue;
echo "<br />";
foreach($change as $text => $info)
{
echo $text." x ".$info[0];
echo "<br />";
}
}
?>
<form action="" method="post">
<label for="cost">Actual Cost:</label>
<input type="number" name="cost" id="cost" />
<label for="given">Money Given:</label>
<input type="number" name="given" id="given" />
<input type="submit" name="formsub" value="Calculate change" />
</form>
</body>
</html>
Let me know if you need any help with it (if you want to use it, of course).