首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 C# 寫個人住房貸款計算機(五)

     編輯:關於C語言

在該類別中覆寫了基類的 Calculate 虛擬方法,在主迴圈中逐月計算還款計劃表。

等本息法在利率不變的情況下,每月的還款額是固定的,所以也稱為「等額法」,計算公式如下:

月還款額 = 貸款金額 × 月利率 × (1 + 月利率)期數 / [(1 + 月利率)期數 - 1]

這個公式在 GetMonthAmount 方法中計算。

而月還利息等於上月剩餘貸款餘額乘以月利率,月還本金等於月還款額減去月還利息。

然後,本月剩餘貸款餘額等於上月剩餘貸款餘額減去月還本金。

最後,由於計算時需要進行捨入處理,到最後一期還款後可能剩餘的貸款餘額不為零,這就需要在保持月還款額不變的情況下調整月還本金和月還利息。

表示等本金法的 LoanDesc 類別也是從 LoanBase 類別中衍生的:

using System;
using System.Drawing;
using System.Collections.Generic;

namespace Skyiv.Ben.LoanCalculator
{
    // 等本金法
    class LoanDesc : LoanBase
    {
        public LoanDesc(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
            : base(balance, months, date, rates)
        {
        }

        protected override void Calculate(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
        {
            decimal baseAmount = Round(balance / months), totalAmount = 0, totalInterest = 0;
            for (int month = months; month >= 1; month--, date = date.AddMonths(1))
            {
                var monthRate = GetMonthRate(date, months, rates);
                var interest = Round(balance * monthRate);
                var monthAmount = baseAmount + interest;
                balance -= baseAmount;
                if (month == 1 && balance != 0)
                {
                    baseAmount += balance;
                    monthAmount += balance;
                    balance = 0;
                }
                totalAmount += monthAmount;
                totalInterest += interest;
                Table.Rows.Add(new object[] { months - month + 1, date, baseAmount, interest, monthAmount, balance, totalAmount, totalInterest });
            }
        }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved