# Breaking down a long math calculation

Well, when you have the percentage, such as 0.056034, you multiply it by 100 to end up with a visual representation of the percentage, that being 5.6034%

For example,

``````var aprRate = 0.056034,
aprPercentage = Math.toFixed(aprRate * 100, 2) + '%';
``````

@Paul_Wilkins sorry to sound so noob, whats the ,2 for and what does comma (,) do in javascript?, plus when i do Math.Fixed(), it says its not a function?

The comma 2 is for a second parameter to the toFixed function, specifying to how many decimal places we want our final percentage value to be shown as.

The Fixed() part has been fixed to be toFixed() instead, no pun intended.

@Paul_Wilkins when i do

aprPrecentage = Math.toFixed(totalRepayable * 100,2);

it says Math.toFixed is not a function, i seen it before, why does it take () as function and variable in it as function args?

Ahh, my bad on that, it was late sorry. The following will work for you, assuming that totalRate is a percentage in decimal form.

``````totalRate = 0.0564545435;
aprPrecentage = (totalRate * 100).toFixed(2);
``````

There are a few different ways of using it. The toFixed function is a part of the Number object. The toFixed function gives a string version of the number, so that some formatting can be applied. The argument that you give to toFixed says to how many decimal places the string version of should be shown as.

@Paul_Wilkins I get a seriously big amount. like 125789.60%, shouldnât be the APR just something atmost like 5.99%, i mean the value such big is like a APR rip off. lol

what is the current APR calculation you use?

@Dormilich This but my base rate is based of user input.

then this function should already give you the correct APR.

@Paul_Wilkins here @Dormilich is my complete code, wonder why my code gives a APR of 5.69821268% etc. I pasted it in pastebin as i didnât wanted to be indexed. [ot]Sorry, i created a new thread, was checking + reply as linked topic link[/ot]

With the code that you have just provided, I see where you are calculating the total repayable, but I donât see where you are calculating the APR rate.

Youâve mentioned this a few times, and I think that you may need to learn more about indexing.

Google indexes pastebin along with other websites. This can be demonstrated with a few well-chosen keywords that are somewhat unique to your code, such as:

execllent eneterd

Youâll see the code from 5 days ago there, and before too long the more recent code that you pasted there will be seen too.

[ot] I thought, they delete that after expiration date[/ot], I am kind of lost in the APR, donât know what baserate is if i apply the code you mention and some other variables are keeping me lost (which one should i replace with mine)

Off Topic:

what you think of the code i wrote, is it clean and good.

thereâs room for improvement.

• checkboxes do not restrict how many you click => use radio buttons with a common name
• then you can save the percentages in each radio button
• `rating` is already a number, no need to use `partseInt()` on it. for number you would use `Math.floor()` / `Math.ceil()`
• the last 2 lines of at the broker fee are identical => move that out of the condition
• the second condition at the broker fees do not need an extra condition since the current condition is the only possible condition left => use `else` instead of `else if`
• select elements have a value property themselves, no need to use selectedIndex

Sorry no. I must sleep now, but tomorrow Iâll spend a few hours going through the problems, and what can be done about them.

Would you mind easing the burden by providing the html form code that goes with the code, so that I donât end up creating a separate version of it? That will help to ensure that my feedback is as relevant to your situation as possible.

@Paul_Wilkins I was able to find a example, that somehow works. But, it gives me negative apr if my rating is poor and amount is 20000 over year of 16 years. Like it gives me negative values. wonder why

For that, I would need to understand the APR formulas that you are wanting to use.

Can you please update me (and perhaps the rest of us) on the math of what is supposed to happen, based on things like the rating and the number of years?

As an example, to figure out the APR we start with the following information:

i = the yearly rate of interest as a fraction
q = the number of times per year that the interest is compounded

The annual percentage rate is calculated using the following formula.
r = (1+[i/q])^q - 1

With an interest rate of 18%, and compounding monthly, that gives:
i = 0.18
q = 12

Which means r = 0.195618, which is an APR of 19.5618 percent.

That is how APR works, and is calculated, and gives a function similar to the following:

``````function aprRate(interestRate, compoundingPeriods) {
// r = (1+[i/q])^q - 1
return Math.pow(1 + (interestRate / compoundingPeriods), compoundingPeriods) - 1;
}
``````

What I want to know from you is what other things that you are needing to figure out?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.