平衡计算还款的利息

时间:2017-05-24 09:26:29

标签: c# math finance

我正在尝试设计一些代码,用于计算贷款的还款计划,给定原则金额,期限(12个月)和利率。我希望每个月的付款价值相同(最后一次可以略有不同以考虑剩余款项),并且利息是根据剩余的贷款余额计算的。

基本规则是

  • 利息取决于贷款余额。
  • 贷款余额取决于每月还款总额。
  • 资本还款取决于每月还款总额和利息。
  • 每月还款总额可能会有所不同,但每月必须相同(最后一个可能不同)。

这是我目前拥有的代码,它会连续迭代,直到找到正确的每月还款总额。这确实有点工作,但看起来非常低效,并且当它不应该时,它可能会非常平衡地变成负面。我想知道是否有人知道更好的方法吗?

 static void calcRepaymentPlan()
    {
        decimal loanAmount = 100000; //100,000
        decimal ir = 0.10m;

        //day counts
        int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        //get month/days
        int dayCount = days.Sum();
        int months = days.Length;

        //get interest values
        decimal interestdaily = ir / dayCount;

        //total each month
        decimal ideal_total_repayment = (loanAmount / months); //random number but seems like the best place to start

        //some arrays to store balances
        decimal[] balances = null;
        decimal[] interest = null;
        decimal[] capitalrepayment = null;

        decimal totalRepaymentsValue = 0;
        while (totalRepaymentsValue != loanAmount)
        {
            //array for balance each month
            balances = new decimal[months];
            for (int i = 0; i < months; i++)
            {
                balances[i] = loanAmount - ((i+1) * ideal_total_repayment);
            }

            //array for interest each month
            interest = new decimal[months];
            for (int i = 0; i < months; i++)
            {
                interest[i] = (interestdaily * balances[i]) * days[i];
            }

            //array for capital each month
            capitalrepayment = new decimal[months];
            for (int i = 0; i < months; i++)
            {
                capitalrepayment[i] = ideal_total_repayment - interest[i];
            }

            //how much of the loan are we paying back?
            totalRepaymentsValue = capitalrepayment.Sum();

            //how much difference between the loan amount and the total we have got now?
            decimal difference = loanAmount - totalRepaymentsValue;

            //how much to increment 
            decimal incr = 1;

            //are we within 100? if so make the increment finer
            if (difference < 100)
                incr = 0.1m;

            //if the difference is less than 1, then just add/subtract it from the last repayment
            if (difference < 1)
            {
                capitalrepayment[months - 1] += difference;
                balances[months - 1] += difference;

                //this should now be the 
                totalRepaymentsValue = capitalrepayment.Sum();

                continue;
            }

            //change the ideal total repayment
            ideal_total_repayment += totalRepaymentsValue < loanAmount ? incr : -incr;
        }

        Console.WriteLine("--------------------------------------------------------------------------------------");
        Console.WriteLine("   Balance              Capital         Interest           Total Repayment");

        for (int i = 0; i < months; i++)
        {
            string balanceStr = balances[i].ToString("#,##0.00");
            string capitalStr = capitalrepayment[i].ToString("#,##0.00");
            string interestStr = interest[i].ToString("#,##0.00");
            string totalStr = (capitalrepayment[i] + interest[i]).ToString("#,##0.00");

            Console.WriteLine("{0}). {1}         {2}        {3}          {4}", (i + 1),
                balanceStr, capitalStr, interestStr, totalStr);
        }

        Console.WriteLine("Ideal Total Repayment Value : {0}", ideal_total_repayment);
        Console.WriteLine("Total Repaying : {0}", totalRepaymentsValue);
        Console.WriteLine("-------------------------------------------------------------------------------------");


    }

0 个答案:

没有答案