Velocity Reviews > Java > assistance with Java code - calculations

# assistance with Java code - calculations

Jason
Guest
Posts: n/a

 04-22-2005
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

Boudewijn Dijkstra
Guest
Posts: n/a

 04-22-2005
"Jason" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) om...
> 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.

Gordon Beaton
Guest
Posts: n/a

 04-22-2005
On 22 Apr 2005 01:47:33 -0700, Jason wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Betty
Guest
Posts: n/a

 04-22-2005

"Jason" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

Dag Sunde
Guest
Posts: n/a

 04-22-2005
"Betty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Jason" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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.
>

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

--
Dag.

Betty
Guest
Posts: n/a

 04-22-2005

"Dag Sunde" <(E-Mail Removed)> wrote in message
news:5j9ae.5613\$(E-Mail Removed)...
> "Betty" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Jason" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > 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.
> >

>
> And interestRate, is it the number 5.0, or 0.05?
>
> --
> Dag.

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

Dag Sunde
Guest
Posts: n/a

 04-22-2005
"Betty" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Dag Sunde" <(E-Mail Removed)> wrote in message
> news:5j9ae.5613\$(E-Mail Removed)...
> > "Betty" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > >
> > > "Jason" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed) om...
> > > > 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.
> > >

> >
> > And interestRate, is it the number 5.0, or 0.05?
> >
> > --
> > Dag.

>
> 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...

--
Dag.

Hal Rosser
Guest
Posts: n/a

 04-22-2005

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)

chamelionboy@optusnet.com.au
Guest
Posts: n/a

 04-23-2005
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