Interest rate formula

3

I have to do this little work in PHP structured below, but I have a problem there in the interest rate formula, I did not understand how to apply it, I did like this:

/* Calcula os Juros
------------------------------------------------------------- */
function juros($dados){
    $r = (($dados['preco'] * (1 + $dados['txjuros']) ^ $dados['parcelas'] ) * $dados['txjuros']) / ((1 + $dados['txjuros']) ^ $dados['parcelas'] - 1);
    return $r;
}

The correct formula is in the image below:

Secondattempt:

Imadeanalgorithmwithadifferentformula,butthentheinterestwasfixed,regardlessofthenumberofparcels.

Thevalueofinterestshouldrisebyasmuchasapercentageofthevalueofeachparcel.

IfIput4installmentsisthesameamountofinterestthatifIput10installments,thenitiswrongso,shouldsetxinterestrateforfirstinstallmentandvalueyinterestforsecondinstallment.

Someonecanhelpmebyanalyzingmycode,theerrorshouldbeinthelogicofapplyingtheformulagrantedbytheteacher.

PHPcode:

link

    
asked by anonymous 14.09.2014 / 06:27

2 answers

6

To raise some number to another, change the ^ by the function pow ()

 (1 + $dados['txjuros']) ^ $dados['parcelas'] 

Switch By:

 pow( (1 + $dados['txjuros']), $dados['parcelas']) 
    
14.09.2014 / 07:42
3

Complementing the @ lost response.

From PHP 5.6 we can use the new operator to exponentiation .

$r = (($dados['preco'] * (1 + $dados['txjuros']) ** $dados['parcelas']) * $dados['txjuros']) / ((1 + $dados['txjuros']) ** $dados['parcelas'] - 1);

In the future, the pow() function will probably be discontinued in favor of using the new operator.

    
14.09.2014 / 14:29