PHP Compound Interest Calculation

Asked

Viewed 327 times

-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>";
?>
  • 1

    What mistake are you making? What do you need? Be clearer

  • Gabriel, good morning. I just added some more information about the error.

  • Exactly, this kind of confusing question.

1 answer

0


You need to adjust your logic. If I take the initial amount as 7000 and make monthly investments of 500 at 15% interest, in the first month the 15% will be on the initial amount (7000), and the calculated interest (1050) will generate the accumulated for the next month, along with the monthly investment (7000 + 1050 + 500)

Next month, the initial value is 8550. The logic is the same for the other periods.

Below is an example, based on your code.

<?php
$investimento_inicial = 7000;
$investimento_mensal = 500;
$meses = 7;
$taxa_de_juros = 15;

$investimento_acumulado = $investimento_inicial;
$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 + $investimento_mensal;
}

$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>";
?>
  • Bins, thank you so much. I was really forgetting that detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.