# Breaking down a long math calculation

I am trying to get APR with a set of variables. I was able to break down the formula and do calculation, but i bet it is giving me very wrong calculation, as it output a non numeric in result, like 1234e79. I was wonder if my code is wrong?

`````` apr =  Math.pow(1 + exl(Months,0-returnAmount,amount,0),12)-1;
``````

where exl,Maths,returnAmount and amount is a variable and not a function. containing result of some other maths done.
The reason, i have link to the code is its private and don’t want it to be indexed (feel free to jsFiddle)

according to your result, `exl()` returns something around 7,000,000. but since it’s code is private, you have to debug that yourself.

hm, `exl()` looks somewhat like Excel’s PV() function.

@Dormilich
no exl is just variable in javascript., here is the link to code (pls jsFiddle for reply)

why do you treat it as function then?

``````apr = Months,0-returnAmount,amount,0;
``````

this does not do a calculation. it causes a Syntax error.

@Dormilich i just try to make sense of it, and i failed. so hoping someone can help me

The first step would be to know how to calculate the APR mathematically. I looked up APR and found that there slight differences for that (mostly due to legal restrictions).

once you know the math, then you can think on how to implement that in JavaScript.

@Dormilich yes, but the formula i wrote above is the one i wan’t to use. The payment, months/years are flexible based on user input which will contribute to the calculation of apr. so if i can get help with fixing that formula above

``````apr =  Math.pow(1 + exl(Months,0-returnAmount,amount,0),12)-1;
``````

What does the exl function look like?

see post #3, there is no exl() function.

Then the example expression as posted is missing a delimiter or operator of some sort no?

At least the syntax looks off to me.

What is the value of the exl variable?

I’m not seeing it in the pastebin code.

I’d say it’s the `execllentPercent ` variable in PasteBin

Ah, yes, I think you’re right.

As Math.pow() takes 2 parameters - base and exponent - I think the problem is here
`1 + exl(Months,0-returnAmount,amount,0)` i.e.
the commas should be operators (+, -, /, *, etc) and there should also be one between exl and the parenthesized variables.

What they should be, I’m sorry to say, I have no idea

@Mittineague the exl is variable i changed later in pastebin, sorry should have updated before.

That is a numeric result - computers use that format for very huge numbers and very small numbers - the number after the e is the number of places to move the decimal point in the number before the e - the 79 means move the decimal point 79 places to the right in the number before the e to get the answer.

1 Like

If it helps, here’s a jsfiddle I put together using an APR function from an APR formula article.

The jsfiddle that you can play with is at http://jsfiddle.net/pmw57/q0x47by2/

@Paul_Wilkins it make sense, thanks. But APR result is for fixed and It doesn’t show. How does one take APR percentage from the result.

That can be adjusted quite easily, by changing the following returned value:

``````return testrate.toFixed(6);
``````

What doesn’t show? The result?

The resulting value is your APR percentage. For example 0.056034 is the same as 5.6034%

@Paul_Wilkins , so here is my code. I hope i am correct? If so how do i convert that value to percentage?

Currently your code shows the total repayable amount of money. I think that it will help if you can explain to me how that relates to the percentage that you seem to desire.

@Paul_Wilkins I don’t know how APR are calculated. Is it the total repayable amount that is applied for APR or just your input amount you put (without any percentages) etc. Although your code shows and explain, what is being computed but suppose if that is what i am getting. How do get the APR in terms of percentage? In short, i want the total repayable amount and other variables that i have computed or entered by user to calculate percentage of APR on that. like you quoted For example 0.056034 is the same as 5.6034%