assistance with Java code - calculations

J

Jason

Hi there. I am needing a little assistance with something I am working
on.

I have two methods as part of a loan calculator I am building and I
cannot get two calculations working correctly.

assume that I have all variables methods etc in place.

The first code is supposed to calculate the monthly repayment amount
required to pay off a loan at a fixed rate over a fixed term. The
coding I have is:

repayment = principle * interestRate * (Math.pow ((1 + interestRate),
timePeriod)) / (Math.pow ((1 + interestRate), timePeriod) - 1);

I seen to get unusually high figures (ie 6 figure monthly repayments
for a loan of 50000 on a rate of 5%pa over 25 years. Can anyone see
where it is going wrong?

And the second one:

this peice of chode is meant to determine the total amount repaid if a
set repayment is being made at a fixed interest rate. it should also
tell you the number of years and months it takes to pay it off.

while (principle > 0.0){
months = months + 1;
principle = principle * (1 + ((interestRate / 12.0) / 100)) -
repayment;
totalRepayment = totalRepayment + repayment;
}

totalRepayment = totalRepayment + principle;
writer.println("Total repayment amount: $" + totalRepayment);
years = months / 12;
monthRemainder = months % 12;
writer.println("Over " + years + " years and " + monthRemainder + "
months.");

When testing this component i get very strange results. I can get
different results every time using the exact same data.

Can anyone help me?

Thanks
 
B

Boudewijn Dijkstra

Jason said:
Hi there. I am needing a little assistance with something I am working
on.

I have two methods as part of a loan calculator I am building and I
cannot get two calculations working correctly.

assume that I have all variables methods etc in place.

The first code is supposed to calculate the monthly repayment amount
required to pay off a loan at a fixed rate over a fixed term. The
coding I have is:

repayment = principle * interestRate * (Math.pow ((1 + interestRate),
timePeriod)) / (Math.pow ((1 + interestRate), timePeriod) - 1);

I seen to get unusually high figures (ie 6 figure monthly repayments
for a loan of 50000 on a rate of 5%pa over 25 years. Can anyone see
where it is going wrong?

With principle = 50000, interestRate = 0.05 and timePeriod = 25 * 12, I get
repayment = 2500.00.
And the second one:

this peice of chode is meant to determine the total amount repaid if a
set repayment is being made at a fixed interest rate. it should also
tell you the number of years and months it takes to pay it off.

while (principle > 0.0){
months = months + 1;
principle = principle * (1 + ((interestRate / 12.0) / 100)) -
repayment;
totalRepayment = totalRepayment + repayment;
}

totalRepayment = totalRepayment + principle;
writer.println("Total repayment amount: $" + totalRepayment);
years = months / 12;
monthRemainder = months % 12;
writer.println("Over " + years + " years and " + monthRemainder + "
months.");

With repayment from the previous calculation, I get:
Total repayment amount: $52315.869085371414
Over 1 years and 9 months.
When testing this component i get very strange results. I can get
different results every time using the exact same data.

If the variables get reset (to the same value) everytime you run the
calculation, then something is very wrong with your system when you get
different results from them.
 
G

Gordon Beaton

The first code is supposed to calculate the monthly repayment amount
required to pay off a loan at a fixed rate over a fixed term. The
coding I have is:

repayment = principle * interestRate * (Math.pow ((1 + interestRate),
timePeriod)) / (Math.pow ((1 + interestRate), timePeriod) - 1);

I seen to get unusually high figures (ie 6 figure monthly repayments
for a loan of 50000 on a rate of 5%pa over 25 years. Can anyone see
where it is going wrong?

Does interestRate contain 5 or 0.05?

/gordon
 
B

Betty

Jason said:
Hi there. I am needing a little assistance with something I am working
on.

I have two methods as part of a loan calculator I am building and I
cannot get two calculations working correctly.

assume that I have all variables methods etc in place.

The first code is supposed to calculate the monthly repayment amount
required to pay off a loan at a fixed rate over a fixed term. The
coding I have is:

repayment = principle * interestRate * (Math.pow ((1 + interestRate),
timePeriod)) / (Math.pow ((1 + interestRate), timePeriod) - 1);

I seen to get unusually high figures (ie 6 figure monthly repayments
for a loan of 50000 on a rate of 5%pa over 25 years. Can anyone see
where it is going wrong?

And the second one:

this peice of chode is meant to determine the total amount repaid if a
set repayment is being made at a fixed interest rate. it should also
tell you the number of years and months it takes to pay it off.

while (principle > 0.0){
months = months + 1;
principle = principle * (1 + ((interestRate / 12.0) / 100)) -
repayment;
totalRepayment = totalRepayment + repayment;
}

totalRepayment = totalRepayment + principle;
writer.println("Total repayment amount: $" + totalRepayment);
years = months / 12;
monthRemainder = months % 12;
writer.println("Over " + years + " years and " + monthRemainder + "
months.");

When testing this component i get very strange results. I can get
different results every time using the exact same data.
My calculator does these calculations, but for a 5% interest
rate I have to divide by 12 first.
 
D

Dag Sunde

Betty said:
My calculator does these calculations, but for a 5% interest
rate I have to divide by 12 first.

And interestRate, is it the number 5.0, or 0.05?
 
D

Dag Sunde

Betty said:
It is '5' so the value I enter for 'i' is 0.42

Now I'm confused! :)
(Ahh... you're the one with the calculator. Your Calculator
expect the interest in percent pr. period, and not divided by 100...?)

My question was really to the OP... Sorry about that...
 
H

Hal Rosser

be sure to divide annual interest rate by 12
(an interest rate of 6% would be .06/12 or .005)
and to use the number of months (not number of yrs) in the calculations.
(the number of periods for a 30 yr loan would be 30 * 12 = 360)
 
C

chamelionboy

Thank you everyone who replies. you were all of help.

It was the fact that i forgot to add the code to change my annual
interest rates to monthly, and the second problem was that my variables
were retaining the last users values instead of resetting between
multiple uses.

Thank you again everyone.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top