-1
Good night, you guys.
I’m trying to make a simple tax calculator, it’s working except for the question of the pennies that don’t show up at full value.
**
index php.
**
<?php
require ('calculo.php');
?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora de Impostos</title>
</head>
<body>
<h1>Calculadora de Impostos</h1>
<form method="post">
<label for="valor">Valor do produto: </label>
<input type="text" name="valor" pattern="[0-9.,]{1,}" style="text-align:right" placeholder="Ex.: 197.07"><br><br>
<label for="imposto">Imposto do produto (em %): </label>
<input type="text" name="imposto" pattern="[0-9.,-]{1,}" style="text-align:right" placeholder="Somente números"><br><br>
<input type="submit" value="Calcular">
</form> <br>
Valor do Produto (s/ imposto): <?php echo $notax ?><br>
Taxa de Imposto: <?php echo $taxpercentage ?><br>
<hr>
Imposto sobre o produto: <?php echo $taxedResult ?><br>
Valor do produto (c/ imposto): <?php echo $taxprodResult ?><br>
</body>
</html>
**
calculus.php
**
<?php
if (isset($_POST['valor']) && !empty($_POST['valor']) && isset($_POST['imposto']) && !empty($_POST['imposto'])) {
$valor = addslashes($_POST['valor']);
$imposto = addslashes($_POST['imposto']);
// Passando os valors para float
$valor = floatval($valor);
$valor = number_format($valor, 2, ",", ".");
$imposto = floatval($imposto);
$imposto = number_format($imposto, 2, ",", ".");
$notax = "R$ " . $valor;
$taxpercentage = $imposto."%";
$taxed = ($valor * $imposto) / 100;
$taxed = number_format($taxed, 2, ",", ".");
$taxedResult = "R$ " . $taxed;
$taxprod = $valor + $taxed;
$taxprod = number_format($taxprod, 2, ",", ".");
$taxprodResult = "R$ " . $taxprod;
}
I did a search and if I use str_replace to turn the dots into commas instead of format_number it works but it fails to address the numbers right? I’m a beginner in PHP and if anyone can explain this to me, because from what I read in the documentation:
number_format - Format a number with thousands grouped
number_format ( float $number [, int $decimals ] ) stringstr_replace - Replace all occurrences of the search string with the substitution string
str_replace ( Mixed $search , Mixed $replace , Mixed $Subject [, int &$Count ] ) : Mixed
Thanks for your help!