Created
April 24, 2017 02:29
-
-
Save zr0n/01a716cb2c02a2457d11ed404dfccb62 to your computer and use it in GitHub Desktop.
Calcula o troco de um determinado valor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function modulo($num1, $num2 ){ | |
$result = (($num1 / $num2) - floor($num1 / $num2)) * $num2; | |
return (float)number_format($result, 3, '.', ''); | |
} | |
function calc_troco($pago, $conta){ | |
$nota = array('100', '50', '10', '5', '1'); | |
$centavos = array('50', '10', '5','1'); | |
$troco = $pago - $conta; | |
$vlr = $troco; | |
for($i = 0; $i <= 4; $i++){ | |
$ct = floor($vlr / $nota[$i]); // calculando a qtde de notas | |
if ($ct) { | |
$result = $ct. " nota(s) de R$". $nota[$i]." Reais<br>"; | |
$vlr = modulo($vlr, $nota[$i]); // sobra | |
echo $result; | |
} | |
} | |
$vlr = $vlr * 100; | |
foreach($centavos as $cent){ | |
$ct = floor($vlr / $cent); | |
if($ct){ | |
$result = "$ct moeda(s) de {$cent} centavos<br>"; | |
$vlr = modulo($vlr, $cent); | |
echo $result; | |
} | |
} | |
} | |
calc_troco(20, 5.20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment