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) {
try
{
loanAmount = Integer.parseInt(loanAmountText);
years= Integer.parseInt(numberOfYearsText);
a_i_r=Double.parseDouble(AIRText);
for_dec=1;
for(int j=0; j<dec_places; j++)
{
for_dec=for_dec*10;
}
return true;
}
catch(Exception e)
{
return false;
}
}
//calculating Monthly Payment
public String getMonthlyPayment()
{
monthlyRate=a_i_r/1200;
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;
principal[0]=remaining_principle;
for(int i=0; i<years*12; i++)
{
interest[i]=remaining_principle* monthlyRate;
principal[i]=monthlyPymnt -interest[i];
remaining_principle=remaining_principle-principal[i];
balance[i]=remaining_principle;
Object[] row = new Object[4];
row[0]=i+1;
row[1]="$"+Math.floor(interest[i]*for_dec)/for_dec;
row[2]="$"+Math.floor(principal[i]*for_dec)/for_dec;
row[3]="$"+Math.floor(balance[i]*for_dec)/for_dec;
table.addRow(row);
}
return table;
}
}