Interest rate formula

Asked

Viewed 1,096 times

4

I have to do this little work in PHP structured below, but I have a problem there in the formula of interest rate, I didn’t understand how to apply, I did so:

/* 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: fórmulas de taxa de juros


Second attempt:

I made an algorithm with a different formula, but then the interest was fixed, regardless of the amount of installments.

The value of interest should rise by as much % over the value of each instalment.

If I put 4 installments is the same interest value that if I put 10 installments, then it is wrong so, should set value x interest for first installment and value y interest for second installment.

Someone can help me by analyzing my code, the error must be in the logic of applying the formula granted by the teacher.

PHP code:

http://pastebin.com/rGFAxUyR

  • 1

    André, limit yourself to one topic per question. Even if you have the knowledge in financial mathematics, your question is outside the context of the site.

  • 1

    Buddy, tell me what you have to calculate so I can tell you what expression you have to use...

  • Because you put the expression you are using, but you did not put what the goal is. So there is no way we can say if it is correct or not...

  • The application will never work because there is a division of the AMOUNT by the DISCOUNT

2 answers

7

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

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

Exchange for:

 pow( (1 + $dados['txjuros']), $dados['parcelas']) 
  • I understood its formula, but I am applying it in the wrong way, could you inform me if the value it returns would be the total or only the interest ? For it has returned to me a value too high to be a correct result.

4

From PHP 5.6 we can use the operator to exponentiation.

$r = (($dados['preco'] * (1 + $dados['txjuros']) ** $dados['parcelas']) * $dados['txjuros']) / ((1 + $dados['txjuros']) ** $dados['parcelas'] - 1);
  • I understood its formula, but I am applying it in the wrong way, could you inform me if the value it returns would be the total or only the interest ? For it has returned to me a value too high to be a correct result.

  • The formula of your question is wrong then.

Browser other questions tagged

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