Calculate credit payments in C#


[credit]

For each month the program:

  • Calculates the payment first. This is either a percent of the balance or the minimum amount, whichever is greater.
  • Calculates interest after the payment is calculated but before it is subtracted from the balance.
  • Adds the interest to the balance.
  • If the payment is greater than the balance, the payment is set equal to the balance.
  • Subtracts the payment from the balance.
  • Displays the results for the month.

This method agrees well with the calculator at Bizrate.com.

The following code does all the work.

// Calculate the payments.
private void btnGo_Click(object sender, EventArgs e)
{
    // Get the parameters.
    decimal balance =
        decimal.Parse(txtInitialBalance.Text, NumberStyles.Any);
    decimal interest_rate =
        decimal.Parse(txtInterestRate.Text.Replace("%", "")) / 100;
    decimal payment_percent =
        decimal.Parse(txtPaymentPercent.Text.Replace("%", "")) / 100;
    decimal min_payment =
        decimal.Parse(txtMinPayment.Text, NumberStyles.Any);
    interest_rate /= 12;

    txtTotalPayments.Clear();
    decimal total_payments = 0;

    // Display the initial balance.
    lvwPayments.Items.Clear();
    ListViewItem new_item = lvwPayments.Items.Add("0");
    new_item.SubItems.Add("");
    new_item.SubItems.Add("");
    new_item.SubItems.Add(balance.ToString("c"));

    // Loop until balance == 0.
    for (int i = 1; balance > 0; i++)
    {
        // Calculate the payment.
        decimal payment = balance * payment_percent;
        if (payment < min_payment) payment = min_payment;

        // Calculate interest.
        decimal interest = balance * interest_rate;
        balance += interest;

        // See if we can pay off the balance.
        if (payment < balance) payment = balance;
        total_payments += payment;
        balance -= payment;

        // Display results.
        new_item = lvwPayments.Items.Add(i.ToString());
        new_item.SubItems.Add(payment.ToString("c"));
        new_item.SubItems.Add(interest.ToString("c"));
        new_item.SubItems.Add(balance.ToString("c"));
    }

    // Display the total payments.
    txtTotalPayments.Text = total_payments.ToString("c");
}

To model a fixed payment (for example, $200 per month), set Payment % to 0 and set Min Payment to the monthly amount you want to pay.

Note that if you don’t rack up this big debt and you put the payments in a savings account or some other investment, then compound interest will work in your favor instead of against you and you’ll make money instead of paying out a lot of interest. For an example that calculates compound interest with a monthly investment, see Calculate the value of a monthly investment in C#.

Suppose you want to buy something for $5,000.00. Using this example and the default settings, you end up paying $8,109.24 over 138 months. That’s an average payment of $58.76, although in fact you pay a lot more in the beginning because your minimum payment is larger when the balance is bigger.

Instead of putting the $5,000.00 on your credit card, suppose you put $60.00 per month in an account that pays 7.00% interest. After only 69 months you would have saved $5,079.20. You would have enough for the $5,000.00 purchase with a little left over in half the time it would have taken you to pay off the credit card debt and with smaller payments initially!

Of course, there are a couple of catches. First, can you find an investment that pays 7.00% these days? If you only get a 1.00% return, you still raise $5,025.61 in 81 months. That’s not as fast but it’s still a lot faster than paying off the credit card debt and with smaller contributions.

The moral is that you can save a lot of money if you save up for something instead of buying it on credit. You’ll save the interest payments and be finished saving sooner than you would be finished paying off credit card debt.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in algorithms, calculations, finance, mathematics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *