Aug-28-2008

Need help with C# syntax (I’m making a loan calculator)?

loan calculator


I have the following text boxes on my C# windows application:

amountOfLoan
APR
durationOfLoanInYears

and a label called monthlyPayment.

When you click the calculate button, the monthly payment should be displayed in the monthlyPayment label.

Can someone help me with the code that needs to go behind the Calculate button?

IN CASE IT HELPS, THE FOLLOWING FORMULA IS USED TO COMPUTE THE MONTHLY PAYMENT:

payment = (Principle x Rate) / (1 – (1+Rate)) ^ -numberOfMonths

“Payment is equal to principle times rate divided by 1 minus (1 plus rate) to the negative numberOfMonths power”

—-

I just don’t know how to express this in C#….

Posted under Loan calculator
  1. nezticle Said,

    decimal monthlyPayment = (Decimal.Parse(amountOfLoan.text) * Decimal.Parse(APR.text)) / Math.Pow(1 – ( 1 + Decimal.Parse(APR.text)), Decimal.Parse(durationOfLoanInYears.text))m;

    monthlyPayment.text = monthlyPayment.ToString(”c”);

  2. melancholygiant Said,

    Hopefully this does it:

    private void button1_Click(object sender, System.EventArgs e)
    {
    double principal = System.Convert.ToDouble(amountOfLoan.Text);
    double pctrate = System.Convert.ToDouble(APR.Text);
    int monthcount = System.Convert.ToInt32(durationOfLoanInYears.Text);
    double payment = (principal * pctrate) /
    Math.Pow(1 – (1 + pctrate), -monthcount);
    monthlyPayment.Text = payment.ToString();
    }

    The Math.Pow object/method provides the exponent calculation

Add A Comment