0
I’m trying to do the Excel formula VF(future value) in PHP, but the values are not matching, IE, in Excel is a value for the column and in the calculation of PHP come out other values but almost identical values, far from R $ 1,50 to R $ 3,00.
index php.
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-sac">
<style type="text/css">
.table-sac thead tr th,
tbody tr td {
text-align: center;
}
</style>
<thead>
<tr>
<th>Mes</th>
<th>Se for investir</th>
<th>Juros no finan.</th>
<th>Amortização</th>
<th>VT</th>
<th>Valor futuro</th>
</tr>
</thead>
<tbody>
<?php require_once('calcs.php'); ?>
<?php for($i = 0; $i <= $mes; $i++): ?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo number_format($sac['G'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['H'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['I'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo number_format($sac['J'][$i], 2, ',', '.'); ?>
</td>
<td>
<?php echo $sac['K']['vf'][$i]; ?>
</td>
</tr>
<?php endfor; ?>
</tbody>
</table>
</div>
</div>
<!-- row -->
</div>
<!-- container -->
calcs.php
<?php
// F = P. (1+i)° + M.[(1+i)° - 1]/i
function vf($taxa,$n,$pgto,$vlr_presente=0, $iterador)
{
if($iterador > 0):
// $first = bcmul($GLOBALS['ul_vf'], $taxa);
// $second = bcadd($GLOBALS['ul_pgto'], $first);
// $vf = bcadd($GLOBALS['ul_vf'], $second);
// $vf = ($GLOBALS['ul_vf']) + ($GLOBALS['ul_vf'] * $taxa + $GLOBALS['ul_pgto']);
$first = bcadd(1, $taxa);
$second = bcpow($first, $n);
$third = bcmul($GLOBALS['ul_vf'], $second);
$fourth = bcsub($second, 1);
$fifth = bcdiv($fourth, $taxa);
$sixth = bcmul($fifth, $GLOBALS['ul_pgto']);
$seventh = bcadd($third, $sixth);
$vf = $seventh;
$GLOBALS['ul_vf'] = $vf;
$GLOBALS['ul_pgto'] = $pgto;
else:
$vf = $vlr_presente;
$GLOBALS['ul_vf'] = $vf;
$GLOBALS['ul_pgto'] = $pgto;
endif;
return $vf;
}
$vlr_imovel = 144000; //var_dump($vlr_imovel);
$vlr_aluguel = 400.00; //var_dump($vlr_imovel);
$vlr_entrada = 43200.00; //var_dump($vlr_imovel);
$vlr_encargos = 500.00; //var_dump($vlr_imovel);
$mes = 360;
$taxa = (0.54/100);
$se_for_investir = $vlr_encargos + $vlr_entrada;
for($i = 0; $i <= $mes; $i++):
$juros_no_finan = (($vlr_imovel - $vlr_entrada) - ((($vlr_imovel - $vlr_entrada)/$mes)*$i))*0.007;
$amortizacao = ($vlr_imovel - $vlr_entrada)/$mes;
$vlr_total = $amortizacao + $juros_no_finan;
//VALOR FUTURO -------------------------
// Como: FV = PV x ( 1 + i ) ^ n
$n = 1;
$pgto = $vlr_total - $vlr_aluguel;
$vlr_presente = $vlr_entrada + $vlr_encargos;
$tipo = 0;
$one = 0.96;
$two = 0.42;
$taxa2 = 0.54/100;
$vf = vf($taxa2, $n, $pgto, $vlr_presente, $i);
//VALOR FUTURO -------------------------
$sac['G'][] = $se_for_investir;
$sac['H'][] = $juros_no_finan;
$sac['I'][] = $amortizacao;
$sac['J'][] = $vlr_total;
$sac['K']['pgto'][] = $pgto;
$sac['K']['vf'][] = $vf;
endfor;
Explanation of the code.
1 - I loop in the months, 360 months in all.
2 - In the first loop(0) the function FV
is called, FV
receives the parameters:
($taxa, $parcelas, $pagamento, $vlr_presente, $iterador_atual)
These brackets
are from the current loop.
3 - In FV(function) we have conditionals that check whether the $iterador
(which was passed by the parameter) is > 1, example:
if(iterador > 1):
//aqui eu faço a formula com as variaveis globais no else abaixo
//e insiro variaveis globais para ser aproveitadas no proximo loop
else:
//aqui é o primeiro loop, ou seja a primeira linha do excel(o mês 0)
//variavel global desse valor passado(será aproveitada no proximo loop)
//variavel global do pagamento passado(será aproveitada no proximo loop)
endif;
return $valorFuturo;
4 - Now, in Calcs.php we have the future value that will be passed to the view;
The Error here is that the values are not identical, almost, but not identical. I thought it could be the floats, which were disturbing but it is not.
I put the excel file on google drive.
Again, it gives a numerical example of its function. Take a line that gives different and put the values here and how the function call to these values is made. It’s very confusing to understand so much information. For example, the second row of the table looks different, so what is the call in PHP that generates this value?
– Woss
I rephrased the question
– Alex
Please help me, for 2 days with that blessed formula there. ^^_
– Alex
https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83Quest
– Daniel Omine
@Danielomine I did not put the codes here because "I only understand", I even wanted to put, but it is a bit messy, I put in Google Drive also not to leave my question too extensive, so that who wants to solve can run on your computer, understand? But thanks, I’ve never read these tips. And what did you think of my question?
– Alex
The question is ok. The problem is that it depends on a third link the excel file. If that link no longer exists the question loses its meaning. So it becomes out of scope or not clear enough. And even worse, you have something that only you understand, as you said. Maybe better ask on a forum because here the format should be more objective.
– Daniel Omine
@Danielomine understood, I somehow managed to reach a solution through the link that Marceloboni passed below, then I put an answer as a solution, but thank you!
– Alex