I'm trying to see if someone can help me with two issues with my program. It compiles fine without errors, but some of my numbers in my amortization table only gives me one number after the decimal place and others give me two numbers. I was wondering if something in my code is incorrect where it keeps giving me only one number when I am asking for two. I attached the output screen and below is my code so you can see what I am talking about. The second issue is I would like to add a comma between the correct numbers to represent, thousands. For example, the answer on the total payment is $11739.68. How can I get it to say $11,739.68? Any help would be great.

Here is the class that holds all the math.

package loanapplication;

import javax.swing.table.DefaultTableModel;

public class Model {

   public static final String[] COLUMN_NAMES = {
      "Payment", "Interest", "Principal", "Balance"

    int loanAmount;
    int years;
    double a_i_r; //annual interest rate
    double monthlyPymnt;
    double monthlyRate;
    double totalPymnt;
    double[] interest;
    double[] principal;
    double[] balance;
    double remaining_principle;
    int for_dec=1000;

    public boolean initialization(String loanAmountText, String numberOfYearsText, String AIRText, int dec_places) {

            loanAmount = Integer.parseInt(loanAmountText);
            years= Integer.parseInt(numberOfYearsText);
            for(int j=0; j<dec_places; j++)

            return true;
        catch(Exception e)
            return false;


    //calculating Monthly Payment
    public String getMonthlyPayment()
        monthlyPymnt = (loanAmount * (monthlyRate+(monthlyRate/((Math.pow((1+monthlyRate), years*12))-1))));
        return "$"+Math.floor(monthlyPymnt*for_dec)/for_dec+"";

    //calculating Total Payment
    public String getTotalPayment()
       totalPymnt= monthlyPymnt*years*12;
            return "$"+ Math.floor(totalPymnt * for_dec)/for_dec;

    //calculating interest, Principal, Balance value for each month
    public DefaultTableModel get_table()
       DefaultTableModel table = new DefaultTableModel(COLUMN_NAMES, 0);
            interest = new double[years*12];
            principal = new double[years*12];
            balance = new double[years*12];

            remaining_principle= loanAmount;

            for(int i=0; i<years*12; i++)
                interest[i]=remaining_principle* monthlyRate;
                principal[i]=monthlyPymnt -interest[i];

                Object[] row = new Object[4];

            return table;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Attachments Ouput.JPG 92.91 KB

See the API docs for DecimalFormat. Per default it will only print as many decimal places as need to display the value, which it is doing there.

Edited 6 Years Ago by masijade: n/a

You can follow this example.

DecimalFormat nf = new DecimalFormat("#,###,###,###,##0.##");
		return nf.format(your_integer));

But to know more about the DecimalFormat, follow masijade and read the API docs.

This article has been dead for over six months. Start a new discussion instead.