-2
I’m trying to create a compound interest calculator in PHP, where I need to calculate the initial investment value, the monthly investment, the period and by interest percentage each month.
The calculator realizes the sum of the amount initially invested + the amount that will be invested each month + the interest on the two previous values (interest on interest).
Calculator I’m using as a base: https://mepoupenaweb.uol.com.br/simuladores-online-de-investimentos/simulador-de-juros-composto/
EDIT1: the Code is printing a value very different from the site I am based on, for example: if I use the values:
- Initial investment: 7,500
- Monthly investment: 500
- Months: 7
- Interest rate: 15%
My code returns:
- Amount invested: 11,000.00
- Interest: 13.280,16
- Cumulative total: 24,280.16
The site I’m based on returns:
- Amount invested: 11,000.00
- Interest: 761,51
- Total invested: 11,761.51
The figures are very different.
<form method="post" action="juros-compostos.php">
<label for="">Investimento Inicial</label>
<input type="number" name="investimento_inicial">
<label for="">Investimento Mensal</label>
<input type="number" name="investimento_mensal">
<label for="">Por quanto tempo?</label>
<input type="number" name="meses">
<label for="">Rentabilidade <span>Ao Mês</span></label>
<input type="number" name="taxa_de_juros">
<button type="submit">Calcular</button>
<?php
$investimento_inicial = floatval($_POST['investimento_inicial']);
$investimento_mensal = floatval($_POST['investimento_mensal']);
$meses = ($_POST['meses']);
$taxa_de_juros = floatval($_POST['taxa_de_juros']);
$investimento_acumulado = $investimento_inicial + $investimento_mensal;
$investimento_acumulado2 = $investimento_inicial + $investimento_mensal * $meses;
$juros_compostos_total = 0;
for ($i = 0; $i < $meses; $i++) {
$juros_compostos = $investimento_acumulado * $taxa_de_juros / 100;
$juros_compostos_total += $juros_compostos;
$investimento_acumulado += $juros_compostos;
}
$valor_a_receber = $investimento_acumulado2 + $juros_compostos_total;
echo "Valor Investido: " . number_format($investimento_acumulado2, 2, ",", ".") . "<br>" . "<br>";
echo "Total dos juros é: " . number_format($juros_compostos_total, 2, ",", ".") . "<br>" . "<br>";
echo "Total do valor a receber é: " . number_format($valor_a_receber, 2, ",", ".") . "<br>" . "<br>";
?>
What mistake are you making? What do you need? Be clearer
– gabrielfalieri
Gabriel, good morning. I just added some more information about the error.
– Guilherme Silva
Exactly, this kind of confusing question.
– Thiago Lopez