assistance with Java code - calculations

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

  1. Jason

    Jason Guest

    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
    #1
    1. Advertising

  2. "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
    #2
    1. Advertising

  3. 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
    #3
  4. Jason

    Betty Guest

    "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
    #4
  5. Jason

    Dag Sunde Guest

    "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
    #5
  6. Jason

    Betty Guest

    "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
    #6
  7. Jason

    Dag Sunde Guest

    "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
    #7
  8. Jason

    Hal Rosser Guest

    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
    #8
  9. Jason

    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
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ed Bangle
    Replies:
    0
    Views:
    313
    Ed Bangle
    Nov 23, 2003
  2. Replies:
    3
    Views:
    418
    Kevin Spencer
    Jul 1, 2005
  3. Davie

    Remote Assistance In Code

    Davie, Apr 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    534
    Davie
    Apr 13, 2006
  4. tdd

    java date calculations

    tdd, May 14, 2004, in forum: Java
    Replies:
    10
    Views:
    6,588
    Roedy Green
    May 17, 2004
  5. vindictive27

    math calculations, java + asp

    vindictive27, Dec 17, 2007, in forum: Java
    Replies:
    2
    Views:
    440
    Sabine Dinis Blochberger
    Dec 18, 2007
Loading...

Share This Page