-
if else?
I'm trying to get this calculator to show loan payment values even in the event of a 0% interest situation. Presently it just spits out NaN when I use values of 0. I'm guessing I could use an if else around the "return payment" to get it to try another formula if the original goes to zero. How would I write this?
Thanks,
J
Code:
package com.direction.investor.financial.essentials;
import java.io.Serializable;
import java.text.DecimalFormat;
public class LoanCalculator implements Serializable {
private static final long serialVersionUID = 1L;
private double principal;
private double annualRate; // in percentage
private int periodsPerYear;
private int years;
public static void main(String[] args) {
LoanCalculator aCal = new LoanCalculator();
aCal.setPrincipal(200000.0);
aCal.setAnnualRate(8); // 8%
aCal.setPeriodsPerYear(12);
aCal.setYears(30);
System.out.println("Payment=" + aCal.computePayment());
}
public double computePayment() {
double payment = 0;
int totalPeriods = years * periodsPerYear;
payment = principal
* (annualRate/100)
/ (periodsPerYear * (1.0 - Math.pow(1.0 + (annualRate/100)
/ periodsPerYear, -totalPeriods)));
return payment;
}
public String[][] computeAmortization(){
double balance=principal;
double pmt=computePayment();
int totalPeriods=years * periodsPerYear;
double periodInterest=annualRate/(periodsPerYear*100);
double cumulatedPmt=0;
double cumulatedInterest=0;
DecimalFormat aFormat=new DecimalFormat("#,##0.00;(#,##0.00)");
String[][] aRtn=new String[totalPeriods][7];
for(int i=0; i<totalPeriods; i++){
double periodInterestAmt=balance*periodInterest;
cumulatedPmt+=pmt-periodInterestAmt;
cumulatedInterest+=periodInterestAmt;
aRtn[i][0]=String.valueOf(i);
aRtn[i][1]=aFormat.format(balance);
aRtn[i][2]=aFormat.format(pmt-periodInterestAmt);
aRtn[i][3]=aFormat.format(periodInterestAmt);
aRtn[i][4]=aFormat.format(cumulatedPmt);
aRtn[i][5]=aFormat.format(cumulatedInterest);
balance=balance-(pmt-periodInterestAmt);
aRtn[i][6]=aFormat.format(balance);
}
return aRtn;
}
public double getPayment() {
return computePayment();
}
public double getAnnualRate() {
return annualRate;
}
public int getPeriodsPerYear() {
return periodsPerYear;
}
public double getPrincipal() {
return principal;
}
public int getYears() {
return years;
}
public void setAnnualRate(double annualRate) {
this.annualRate = annualRate;
}
public void setPeriodsPerYear(int periodsPerYear) {
this.periodsPerYear = periodsPerYear;
}
public void setPrincipal(double principal) {
this.principal = principal;
}
public void setYears(int years) {
this.years = years;
}
}
-
Re: if else?
Sounds like you're trying to divide by zero or something similar - I would put in a check for special cases (like the 0 case) or any other special edge cases.
-
Re: if else?
Well your culprit is here:
payment = principal * (annualRate/100) / (periodsPerYear * (1.0 - Math.pow(1.0 + (annualRate/100) / periodsPerYear, -totalPeriods)));
But, what happens if the annual rate is negative? I would think you'd want to avoid that too.