首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为每个月的EMI重复整个日期列表

为每个月的EMI重复整个日期列表
EN

Stack Overflow用户
提问于 2021-04-06 11:58:52
回答 1查看 33关注 0票数 0

我在试着做一个摊销时间表。我正在获取数据,但日期显示不正确。对于每个月的利息,所有日期(整个月列表)的本金和余额都会重复。这就是我所得到的

代码语言:javascript
复制
Date    Interest    Principal   Balance
6/04/2021 29        820          9180
6/05/2021 29        820          9180
6/06/2021 29        820          9180
....
6/04/2021 27        822          8358
6/05/2021 27        822          8358
6/06/2021 27        822          8358

对于下一个EMI,整个月份列表被重复,结果应该是这样的

代码语言:javascript
复制
Date    Interest    Principal   Balance
6/04/2021 29        820          9180
6/05/2021 27        822          8358

..。谢谢你的帮助。这是模型类

代码语言:javascript
复制
public class AmortizationSchedule
{
         [DisplayName("Start Date")]
        public DateTime StartDate{get;set;}
        public List<DateTime> Date { get; set; }
        public decimal Interest { get; set; }
        public decimal Balance { get; set; }
        public decimal Principal { get; set; }
}

这就是视图

代码语言:javascript
复制
@model IEnumerable<AmortizationCalculator.Web.Models.AmortizationSchedule>

<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Interest</th>
            <th>Principal</th>
            <th>Balance</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {

            @foreach (var q in item.Date)
            {
                <tr>
                    <td>
                        <li>@q.ToShortDateString()</li>

                    </td>
                    <td>
                        @item.Interest
                    </td>
                    <td>
                        @item.Principal
                    </td>
                    <td>
                        @item.Balance
                    </td>
                </tr>
                }
            }
        </tbody>

</table>

这些是我用来获得EMI的方法,以及开始日期和结束日期之间的月份列表

代码语言:javascript
复制
    public class AmortizedLib
    {

        public List<AmortizationSchedule> AmortizationScheduleCalculation(decimal totalAmount
        , decimal rate, decimal downPayment
            ,int loanTerm, DateTime startDate)
        {
            decimal EMI;
            decimal principal = totalAmount - downPayment;
            decimal monthlyInterest;
            decimal monthlyPrincipal;
            decimal newPrincipalBalance;
            int numberOfEMI = loanTerm * 12;
            int year = loanTerm * 12;

            DateTime Date = DateTime.Now;
            DateTime endDate = Date.AddMonths(year);

            Console.WriteLine(endDate);

            EMI = Math.Round(monthlyPayments(principal, rate, loanTerm));

            List<AmortizationSchedule> schedules = new List<AmortizationSchedule>();
            for (int i = 0; i <= numberOfEMI; i++)
            {
                monthlyInterest = Math.Round((principal * rate / 100) / 12);
                monthlyPrincipal = Math.Round(EMI - monthlyInterest);
                newPrincipalBalance = Math.Round(principal - monthlyPrincipal);
                principal = newPrincipalBalance;

                AmortizationSchedule amortizationSchedule = new AmortizationSchedule();

                    amortizationSchedule.Interest = monthlyInterest;
                    amortizationSchedule.Principal = monthlyPrincipal;
                    amortizationSchedule.Balance = newPrincipalBalance;
                    amortizationSchedule.Date = EachMonth(startDate, endDate, year).ToList();

                schedules.Add(amortizationSchedule);
            }

            return schedules;
        }

        // to calculate monthly EMI
        public static decimal monthlyPayments(decimal actualPrincipal, decimal rate, double loanTerm)
        {
            rate = rate / 1200;
            loanTerm = loanTerm * 12;
            decimal F = (decimal)Math.Pow((double)(1 + rate), loanTerm);
            return actualPrincipal * ((rate * F) / (F - 1));
        }

// to get the time list of  months
        public static IEnumerable<DateTime> EachMonth(DateTime startDate, DateTime endDate , int term)
        {
            int months = term;
            startDate = DateTime.Now.Date;
            endDate = startDate.AddMonths(months);


            for (var day = startDate.Date; day.Date <= endDate.Date; day = day.AddMonths(1))
                yield return day;
        }


    }


  [1]: https://i.stack.imgur.com/5bEo3.png
EN

回答 1

Stack Overflow用户

发布于 2021-04-06 16:07:28

这个错误存在于模型以及calculationMethod中。模型不应该只有List的纯日期属性,foreach循环应该包含整个方法

代码语言:javascript
复制
public class AmortizationSchedule{
        public DateTime Date { get; set; }
        public decimal Interest { get; set; }
        public decimal Balance { get; set; }
        public decimal Principal { get; set; }
}

方法应该是

代码语言:javascript
复制
        public List<AmortizationSchedule> AmortizationScheduleCalculation(decimal totalAmount
        , decimal rate, decimal downPayment
            ,int loanTerm, DateTime startDate)
        {
            decimal EMI;
            decimal principal = totalAmount - downPayment;
            decimal monthlyInterest;
            decimal monthlyPrincipal;
            decimal newPrincipalBalance;
            int numberOfEMI = loanTerm * 12;
            int year = loanTerm * 12;

            DateTime Date = DateTime.Now;
            DateTime endDate = Date.AddMonths(year);

            Console.WriteLine(endDate);

            EMI = Math.Round(monthlyPayments(principal, rate, loanTerm));
            List<AmortizationSchedule> schedules = new List<AmortizationSchedule>();
            int i = 0;
            var monthlyDateList = EachMonth(startDate, endDate, year).ToList();
            foreach (var item in monthlyDateList)
            {
                monthlyInterest = Math.Round((principal * rate / 100) / 12);
                monthlyPrincipal = Math.Round(EMI - monthlyInterest);
                newPrincipalBalance = Math.Round(principal - monthlyPrincipal);
                principal = newPrincipalBalance;

                AmortizationSchedule amortizationSchedule = new AmortizationSchedule();
                amortizationSchedule.Date = item;
                amortizationSchedule.Interest = monthlyInterest;
                amortizationSchedule.Principal = monthlyPrincipal;
                amortizationSchedule.Balance = newPrincipalBalance;
                schedules.Add(amortizationSchedule);
                i++;
            }

            return schedules;

        }

视图应该是

代码语言:javascript
复制
<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Interest</th>
            <th>Principal</th>
            <th>Balance</th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.Date.ToShortDateString()
                </td>
                <td>
                    @item.Interest
                </td>
                <td>
                    @item.Principal
                </td>
                <td>
                    @item.Balance
                </td>

            </tr>
        }
        }
    </tbody>
</table>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66962524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档