1

我有一个贷款摊销代码,我在网上找到并进行了一些修改以满足我的需要(C#)。我遇到的问题是计算包含在循环中的每月利息。它不会返回正确的值。

下面是我的代码(有点长):

 private void CalculateLoan()
        {
            // Make sure we use types that hold decimal places             
            DateTime payDate = DateTime.ParseExact(_startDate.Value, "yyyyMMdd", null);
            double interestRate = 0;
            double monthlyInterest = 0;
            double loanAmount;
            short amortizationTerm = 0;
            double currentBalance;
            double cummulativeInterest = 0;
            double monthlyPrincipal = 0;
            double cummulativePrincipal = 0;

            loanAmount = double.Parse(_principal.Value);
            currentBalance = loanAmount;
            interestRate = double.Parse(_interestRate.Value) * 0.01;
            amortizationTerm = short.Parse(_period.Value);

            // Calculate the monthly payment and round it to 2 decimal places           
            var monthlyPayment = ((interestRate / 12) / (1 - (Math.Pow((1 + (interestRate / 12)), -(amortizationTerm))))) * loanAmount;
            monthlyPayment = Math.Round(monthlyPayment, 2);

            // Storage List
            List<AmortPayment> amortPaymentList = new List<AmortPayment>();

            // Loop for amortization term (number of monthly payments)
            for (int j = 0; j < amortizationTerm; j++)
            {
                // Calculate monthly cycle
                monthlyInterest = currentBalance * interestRate;  **<-----problem here**
                monthlyPrincipal = monthlyPayment - monthlyInterest;
                currentBalance = currentBalance - monthlyPrincipal;

                if (j == amortizationTerm - 1 && currentBalance != monthlyPayment)
                {
                    // Adjust the last payment to make sure the final balance is 0
                    monthlyPayment += currentBalance;
                    currentBalance = 0;
                }

                // Reset Date
                payDate = payDate.AddMonths(1);
                // Add to cummulative totals
                cummulativeInterest += monthlyInterest;
                cummulativePrincipal += monthlyPrincipal;

                amortPaymentList.Add
                    (new AmortPayment 
                    { 
                      RowNumber = j + 1,
                      Date = payDate,
                      ScheduledPayment = Math.Round(monthlyPayment, 2),                                        
                      Interest = Math.Round(monthlyInterest, 2),
                      TotalRepayment = Math.Round(monthlyPayment + monthlyInterest, 2),
                      Balance = Math.Round(currentBalance, 2),   
                      TotalInterest = Math.Round(cummulativeInterest, 2),
                      TotalBalance = Math.Round(currentBalance + cummulativeInterest, 2)
                    });

                // Add values to SAP matrix          
                _rowNo.Value = (j + 1).ToString();
                _date.ValueEx = payDate.ToString("yyyyMMdd");
                _payment.Value = monthlyPayment.ToString();
                _interest.Value = monthlyInterest.ToString();
                _totalRepayment.Value = (monthlyPayment + monthlyInterest).ToString();
                _balancePrincipal.Value = currentBalance.ToString();
                _balanceInterest.Value = cummulativeInterest.ToString();
                _total.Value = (currentBalance + cummulativeInterest).ToString();

                _form.Freeze(true);
                oMatrix.AddRow();
                _form.Update();
                _form.Freeze(false);
            }
        }

有人可以告诉我声明哪里出错了 monthlyInterest = currentBalance * interestRate;吗?任何帮助表示赞赏。

4

1 回答 1

1

您应该使用巨额利率而不是年利率,只需将利率设为 12

monthlyInterest = currentBalance * interestRate / 12;
于 2013-02-08T06:15:29.493 回答