I am developing an Application to essentially figure out the interest and principal on a loan a selected number of payments. I have read that it is best to use BigDecimal to ensure accurate caluations, but
I am not getting correct results and wondering if someone would help me.
The algorithm to calculate period principal and period interest is :
peridocicInterest amount = Remainging Balance * periodic interest rate
periodicPrincipal amount = payment - peridocicInterest amount
Remainging Balance = Remainging Balance - periodicPrincipal amount
public class Loan {
private double payment;
private double interestRate;
private int amoritizationTerm;
private int actualTerm;
private double loanAmount;
private double downPayment;
private double remaingingBalance;
public Loan(double interestRate, int amoritizationYears, int actualTerm, double loanAmount, double downPayment) {
amoritizationTerm = amoritizationYears * 12;
this.interestRate = (interestRate/100.00)/12;
this.actualTerm = actualTerm;
this.loanAmount = loanAmount;
this.downPayment = downPayment;
remaingingBalance = this.loanAmount - this.downPayment;
public double calculatePayment()
{
double tmpPayment = Math.abs(FinanceLib.pmt(interestRate, amoritizationTerm, loanAmount, 0, false));
BigDecimal bdPayment = new BigDecimal(tmpPayment);
bdPayment = bdPayment.setScale(2, RoundingMode.HALF_EVEN);
System.out.println(bdPayment);
return payment = bdPayment.doubleValue();
}
public double amoritzeLoan()
{
BigDecimal bdPayment;
BigDecimal bdRemainingBalance = new BigDecimal(remaingingBalance);
if(payment <= remaingingBalance)
{
bdPayment = new BigDecimal(payment);
}
else
{
bdPayment = new BigDecimal(remaingingBalance);
}
bdPayment = bdPayment.setScale(2, RoundingMode.HALF_EVEN);
//Periodic Interest Rate
BigDecimal bdInterest = new BigDecimal(interestRate);
bdInterest = bdInterest.setScale(13, RoundingMode.HALF_EVEN);
//Calculate Amount of Interest Charged in Period
BigDecimal bdPeriodInterest = new BigDecimal(0);
bdPeriodInterest = bdRemainingBalance.multiply(bdInterest);
bdPeriodInterest = bdPeriodInterest.setScale(2, RoundingMode.HALF_EVEN);
//Calculate Principal gained in Period
BigDecimal bdPeriodPrincipal = new BigDecimal(0);
bdPeriodPrincipal = bdPayment.subtract(bdPeriodInterest).setScale(2, RoundingMode.HALF_EVEN);
System.out.println("Period Interest " + bdPeriodInterest);
System.out.println("Period Interest " + bdPeriodPrincipal);
//Create Amoritization Table
for(int i = 0; i < amoritizationTerm; i++)
{
//Check that Remainging Balance isn't < Payment
if(bdPayment.doubleValue() > bdRemainingBalance.doubleValue())
{
bdPayment = bdRemainingBalance;
}
//Calculate Amount of Interest Charged in Period
bdPeriodInterest = new BigDecimal(0);
bdPeriodInterest = bdRemainingBalance.multiply(bdInterest);
bdPeriodInterest = bdPeriodInterest.setScale(2, RoundingMode.HALF_EVEN);
//Calculate Principal gained in Period
bdPeriodPrincipal = new BigDecimal(0);
bdPeriodPrincipal = bdPayment.subtract(bdPeriodInterest).setScale(2, RoundingMode.HALF_EVEN);
bdRemainingBalance = bdRemainingBalance.subtract(bdPeriodPrincipal);
System.out.println("Principal = " + bdPeriodPrincipal + " Interest = " + bdPeriodInterest + " Balance = " + bdRemainingBalance);
//System.out.println("Principal = " + bdPeriodPrincipal + " Interest = " + bdPeriodInterest );
}
//Interest
return bdPayment.subtract(bdPeriodInterest).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
}
}
Output from the for the first 3 payments is for loan amount 50000 at 6 % interest for 30 years
Principal = 49.78 Interest = 250.00 Balance = 49950.22
Principal = 50.03 Interest = 249.75 Balance = 49900.19
Principal = 50.28 Interest = 249.50 Balance = 49849.91
*The principal and interest and Balance amount are wrong starting with the 2nd payment. Does anyone have any idea what I did wrong?*