Results 1 to 5 of 5
  1. #1
    neverbend is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Rounding Decimal Places

    Hi

    I am have been asked at college to write a programme that will calculate monthly repayments and total repayments on a loan. I have that done. I am now looking for a way to round to two deciaml places as my monthly repayment is coming up with about ten decimals places. Any suggestions on this would help, my code is below, also if you could tell me where to insert the code for this, it would be a great help!!

    class Loan_Interest

    {
    public static void main(String args[]);

    {
    double intRate; //Interest rate
    double initLoan; //Initial Loan Amount
    double loanTerm; //Full Term of Loan
    double monRepay; //Monthly repayments
    double totRepay; //Total Loan repayment including interest

    System.out.println("Please enter full loan amount ");
    initLoan = EasyIn.getDouble();

    System.out.println("Please enter interest rate, eg if the interest rate is 5%, enter 0.05 ");
    intRate = EasyIn.getDouble();

    System.out.println("Please enter the loan term in years ");
    loanTerm = EasyIn.getDouble();

    monRepay = ((initLoan * intRate) / 12) + ((initLoan / loanTerm) / 12);
    totRepay = (monRepay * 12) * loanTerm;

    System.out.println("Your monthly repayments are " + monRepay);

    System.out.println("The total loan to be repayed over the full term of the loan is " + totRepay);
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Rounding Decimal Places

    Sounds like a job for DecimalFormat.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kalambekar is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Rounding Decimal Places

    place following piece of code after the line where you have assigned the value for monRepay


    monRepay = ((initLoan * intRate) / 12) + ((initLoan / loanTerm) / 12);

    double monRepayNew = rounding(monRepay,2);


    totRepay = (monRepayNew * 12) * loanTerm;

    System.out.println("Your monthly repayments are " + monRepayNew);

    System.out.println("The total loan to be repayed over the full term of the loan is " + totRepay);
    }

    public static double rounding(double monRepayNew,int place){

    double d = (double)Math.pow(10,place);
    monRepayNew = monRepayNew * d;
    double value = Math.round(monRepayNew);
    return (double) value/d;
    }
    }

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default Re: Rounding Decimal Places

    Use printf instead of pintln...or or or...
    System.out.printf("The total loan to be repayed over the full term of the loan is %.2f \n", totRepay);

  5. #5
    neverbend is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Rounding Decimal Places

    thanks folks, perfect, exactly what i needed!

Similar Threads

  1. Rounding decimal error
    By NixasMuraki in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 01:07 AM
  2. 3 Decimal Places
    By benhawk in forum New To Java
    Replies: 7
    Last Post: 11-02-2010, 09:28 PM
  3. 2 decimal places needed
    By lala in forum New To Java
    Replies: 5
    Last Post: 10-27-2010, 08:13 PM
  4. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  5. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •