Stephandeye
06-30-2009, 06:25 AM
I built a calculator that lists monthly mortgage payments, loan balance and interest paid throughout the life of 3 diff mortage loans. All of the loans should end at a balance of $0.00. However, one of the loans ends at a balance of -$0.00 (which can't logocally exist in currency). I played with my decimal formatting and discovered to acutal ending balance (according to my calculator) is -$0.0000000055. How can I make this number 0 without affecting all of my other calcualtions? and isn't the decimal format method supposed to round numbers automatically anyways?
I will be honest, this is homework. However, this project is done other that this one minor detail, which is more of annoyance than anything else. Any suggestion or points in right direction would be appreciated. Please DO NOT give me the answer, I want to LEARN this on my own. However, my anal retentive self will not be satisfied till I can get rid of this -$0.00. Once again any pointers would be appreciated.
Here is my code:
[CODE]
import java.text.DecimalFormat;
public class MortgageCalc
{
public static void main(String [] args) throws InterruptedException
{
double [] rate = {5.35, 5.5, 5.75};
int [] years = {7, 15,30};
int [] loan_num = {1, 2, 3};
int dollars = 200000;
int year_num = 1;
int pay_num = 1;
int month;
double tax;
double tax_rate, month_tax, monthly_pay, pay_interest, balance;
DecimalFormat twoDigits = new DecimalFormat("$###,##0.00");
System.out.println();
System.out.println("\t\t Monthly Mortgage Payment Calculator");
for (int i=0; i<rate.length; i++)
{
Thread.sleep(2000);
tax = (rate [i] / 100) / 12;
tax_rate = tax + 1;
month = years [i] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);
pay_interest = tax * dollars;
balance = dollars - (monthly_pay - pay_interest);
System.out.println("\t\t\t\tLoan: " + (loan_num [i]));
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [i]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [i]) + "%");
System.out.printf("\t\t\tMonthly Payment: " + twoDigits.format(monthly_pay));
System.out.println();
year_num =1;
for (int j=0; j<years[i]; j++)
{
Thread.sleep(2000);
System.out.println();
System.out.println();
System.out.println("\t\t\t\tYear " + (year_num));
System.out.println();
System.out.println("Monthly Payment: \tInterest Paid: \t\tLoan Balance:");
pay_num = 1;
year_num = year_num + 1;
for (int k = 0; k < 12; k++)
{
System.out.printf("\t" + (pay_num) + "\t\t " + twoDigits.format(pay_interest) + "\t\t "+ twoDigits.format (balance) + "\n");
pay_num = pay_num + 1;
pay_interest = tax * balance;
balance = balance - (monthly_pay - pay_interest);
}
}
}
}
[CODE]
I will be honest, this is homework. However, this project is done other that this one minor detail, which is more of annoyance than anything else. Any suggestion or points in right direction would be appreciated. Please DO NOT give me the answer, I want to LEARN this on my own. However, my anal retentive self will not be satisfied till I can get rid of this -$0.00. Once again any pointers would be appreciated.
Here is my code:
[CODE]
import java.text.DecimalFormat;
public class MortgageCalc
{
public static void main(String [] args) throws InterruptedException
{
double [] rate = {5.35, 5.5, 5.75};
int [] years = {7, 15,30};
int [] loan_num = {1, 2, 3};
int dollars = 200000;
int year_num = 1;
int pay_num = 1;
int month;
double tax;
double tax_rate, month_tax, monthly_pay, pay_interest, balance;
DecimalFormat twoDigits = new DecimalFormat("$###,##0.00");
System.out.println();
System.out.println("\t\t Monthly Mortgage Payment Calculator");
for (int i=0; i<rate.length; i++)
{
Thread.sleep(2000);
tax = (rate [i] / 100) / 12;
tax_rate = tax + 1;
month = years [i] * 12;
month_tax = Math.pow (tax_rate, month);
monthly_pay = ((dollars * month_tax) * tax) / (month_tax - 1);
pay_interest = tax * dollars;
balance = dollars - (monthly_pay - pay_interest);
System.out.println("\t\t\t\tLoan: " + (loan_num [i]));
System.out.println("\t\t\tLoan Amount: " + twoDigits.format(dollars));
System.out.println("\t\t\tDuration of loan: " + (years [i]) + " years");
System.out.println("\t\t\tInterest Rate: " + (rate [i]) + "%");
System.out.printf("\t\t\tMonthly Payment: " + twoDigits.format(monthly_pay));
System.out.println();
year_num =1;
for (int j=0; j<years[i]; j++)
{
Thread.sleep(2000);
System.out.println();
System.out.println();
System.out.println("\t\t\t\tYear " + (year_num));
System.out.println();
System.out.println("Monthly Payment: \tInterest Paid: \t\tLoan Balance:");
pay_num = 1;
year_num = year_num + 1;
for (int k = 0; k < 12; k++)
{
System.out.printf("\t" + (pay_num) + "\t\t " + twoDigits.format(pay_interest) + "\t\t "+ twoDigits.format (balance) + "\n");
pay_num = pay_num + 1;
pay_interest = tax * balance;
balance = balance - (monthly_pay - pay_interest);
}
}
}
}
[CODE]