Some Suggestions, Please

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Some Suggestions, Please

  1. #1
    Join Date
    Jan 2006

    Some Suggestions, Please

    I need to create an Interest class that calculates monthly mortgage payments. After calculating the payment, the value, which is a double, must be rounded to 2 decimal places using a method called "round". This method must return the rounded value. The last idea I had was to convert the double to a string, format the string with DecimalFormat, and then converting the formatted string to a double and returning that value. Here's the code I have so far.

    class Interest
    private double principal; // Loan or mortgage amount
    private double percent; // yearly interest entered in percents
    private int years; // Number of years to pay back the loan
    private double monthlyPmt; // Monthly loan payment computed by provided formula
    private int months; // Number of months to pay back the loan
    private double monthlyRate; // Monthly interest rate
    private double totalPmt; // Total amount of the loan payments
    private double totalInt; // Total amount of interest paid

    public Interest(double la, double yi, int py)
    // Constructor
    // Assigns values for Loan Amount, Yearly Interest and Payback Years
    // Constructor should also compute the MonthlyRate and number of Months

    principal = la;
    percent = yi;
    years = py;
    months = years * 12;
    monthlyRate = (yi/100)/12;
    totalPmt = monthlyPmt * months;
    totalInt = totalPmt - principal;


    public void computePayment()
    // void methods which computes the monthly loan payment with the provided formula.
    double n1;
    int n2;
    double n3;
    n1 = 1 + monthlyRate;
    n2 = months;
    n3 = Math.pow(n1,n2);
    monthlyPmt = ((monthlyRate * n3) / (n3 - 1)) * principal;

    private double round(double x)
    // method which returns x rounded off to the nearest 1/100th
    DecimalFormat fmt = new DecimalFormat("0000000.00");
    x = monthlyPmt;
    String s = Double.toString(x);
    s = fmt.format(s);
    x = Double.parseDouble(s);
    return x;


    public void displayResults()
    // void method, which displays the attribute values of the Interest object.
    System.out.println("Loan Amount: " +principal);
    System.out.println("Yearly Interest: " +percent);
    System.out.println("Payback Years: " +years);
    System.out.println("Monthly Payment: " +monthlyPmt);
    System.out.println("Total Payments: " +totalPmt);
    System.out.println("Total Interest: " +totalInt);

  2. #2
    Join Date
    Aug 2005
    Have u considered the Ceiling or the Floor methods ????????

  3. #3
    Join Date
    Nov 2004
    You can do it like:
    multiply the double value by 100, add 0.5, store in a long variable, then divide the long (cast to double) by 100 and store it in the double again.

    However, this method has its limitations when it comes to huge numbers and many decimal places, but, we're talking money here so it should be sufficient.

    Here is a general version that takes the number of decimals as parameter:

       * method that returns x rounded off to the nearest 1/10**nOfDecimals
      private double round(double x, int nOfDec) {
        double fact=Math.pow(10,nOfDec);
        long val=(long)(x*fact+0.5);
        return (double)val/fact;
    But, be warned ! When your program writes down its monthly rates in columns that are totalled on bottom lines, and the user flips out his pocket calculator and checks it, then you have some explaining to do.

    When I was responsible for the monthly gas bill sent to Ruhrgas (appr. 80 mill DM) the discrepancies found could easily amount to 40,000 euro. We named the problem Pocket Calculator Simulation, it was hard work....
    Last edited by sjalle; 01-15-2006 at 07:20 AM.
    eschew obfuscation

  4. #4
    Join Date
    Dec 2005
    New Jersey
    Another way to approach this could be to use the BigDecimal class:
    double round(double x, int place) {
        BigDecimal bd = new BigDecimal(x);
        bd = bd.setScale(place, RoundingMode.HALF_UP);
        return bd.doubleValue();

Similar Threads

  1. Replies: 2
    Last Post: 06-29-2002, 03:02 PM
  2. Re: Thank you for the suggestions
    By Bassel Tabbah in forum VB Classic
    Replies: 0
    Last Post: 08-11-2001, 03:01 PM
  3. Suggestions!
    By Aamir Khan in forum Careers
    Replies: 4
    Last Post: 03-06-2001, 12:17 PM
  4. xBase suggestions for VB6?
    By Dan Rhea in forum VB Classic
    Replies: 0
    Last Post: 04-13-2000, 07:46 AM
  5. Data engine suggestions for VB4 to VB6 conversion.
    By Dan Rhea in forum VB Classic
    Replies: 0
    Last Post: 04-12-2000, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center