Reputation: 43
I'm creating an amortization process using C#. But I'm having a problem why I get a wrong answer in the interest, principal & balance.
Here's my C# Amortization Screenshot.
Its should be like this anyway.
Here's my C# code.
double PV = Convert.ToDouble(txt_principal.Text);
double NPER = Convert.ToDouble(txt_nop.Text);
double RATE = Convert.ToDouble(txt_irpp.Text);
double PMT = Convert.ToDouble(txt_payment.Text);
String PMT_string = PMT.ToString("#,###.##");
String RATE_string = RATE.ToString("#,###.##");
String NPER_string = NPER.ToString("#,###.##");
String PV_string = PV.ToString("#,###.##");
int count = 0;
for (count = 0; count < NPER; count++)
{
dgv_table.Rows.Add(1);
int numrows = count + 1;
dgv_table.Rows[count].Cells[0].Value = numrows;
dgv_table.Rows[count].Cells[1].Value = PMT_string;
double interest = PV * RATE / 100;
double principal = PMT - RATE;
if (PV < PMT)
{
principal = PV;
PMT = RATE + principal;
}
PV = PV - principal;
if (PV < 0)
{
principal = principal + PV;
RATE = RATE - PV;
PV = 0;
}
dgv_table.Rows[count].Cells[2].Value = interest.ToString("#,###.##");
dgv_table.Rows[count].Cells[3].Value = principal.ToString("#,###.##");
dgv_table.Rows[count].Cells[4].Value = PV.ToString("#,###.##");
Any help sir. Why the value of my interest, principal & balance are wrong?
Upvotes: 0
Views: 564
Reputation: 34218
The first problem I can see is in this line:
double principal = PMT - RATE;
This is taking your interest rate away from the payment to derive the principle. That's why in the first line, you have 14,544.16
instead of 11,047.66
as the principle. Switch it to this instead:
double principal = PMT - interest;
EDIT: fixing the above does fix all of your results. (Whilst I hope this has helped, I've still voted to close the question: it's not really a good fit for StackOverflow's format as it's unlikely to be of use to anyone else.)
Upvotes: 1