assistance with Java code - calculations

Discussion in 'Java' started by Jason, Apr 22, 2005.

1. JasonGuest

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

Jason, Apr 22, 2005

2. Boudewijn DijkstraGuest

"Jason" <> schreef in bericht
news:...
> 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.

Boudewijn Dijkstra, Apr 22, 2005

3. Gordon BeatonGuest

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

Gordon Beaton, Apr 22, 2005
4. BettyGuest

"Jason" <> wrote in message
news:...
> 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.

Betty, Apr 22, 2005
5. Dag SundeGuest

"Betty" <> wrote in message
news:...
>
> "Jason" <> wrote in message
> news:...
> > 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.

Dag Sunde, Apr 22, 2005
6. BettyGuest

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

Betty, Apr 22, 2005
7. Dag SundeGuest

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

Dag Sunde, Apr 22, 2005
8. Hal RosserGuest

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)

Hal Rosser, Apr 22, 2005
9. Guest

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

, Apr 23, 2005