你的位置:首页 > ASP.net教程

[ASP.net教程]C#实现的等额本息法、按月付息到期还本法、一次性还本付息法

你若懂行,那便有用,如下:

void Main()
{
    var x = DengEBenXi.Compute(11111, 12, 3);
    x.Dump();
    var y = AnYueFuxiDaoqiHuanBen.Compute(11111, 12, 3);
    y.Dump();
    var z = YicixingHuanBenFuxi.Compute(11111, 12, 3);
    z.Dump();
}
    public class DengEBenXi
    {
        /// <summary>
        /// 等额本息法
        /// </summary>
        /// <param name="amountT">投资金额</param>
        /// <param name="yearRate">年利率</param>
        /// <param name="monthsx">投资期限,单位:月</param>
        /// <returns></returns>
        public static List<BackUnit> Compute(double amount, double yearRate, int months)
        {

            var monthRate = (yearRate) / 1200.0;     //年利率转为月利率
            var datalist = new List<BackUnit>(); 
            var i = 0;
            var a = 0.0; // 偿还本息
            var b = 0.0; // 偿还利息
            var c = 0.0; // 偿还本金
            //利息收益
            var totalRateIncome =
                (amount * months * monthRate * Math.Pow((1 + monthRate), months)) / (Math.Pow((1 + monthRate), months) - 1) - amount;
            var totalIncome = totalRateIncome + amount;
            var d = amount + totalRateIncome; // 剩余本金

            totalRateIncome = Math.Round(totalRateIncome * 100) / 100;// 支付总利息
            totalIncome = Math.Round(totalIncome * 100) / 100;
            a = totalIncome / months;    //每月还款本息
            a = Math.Round(a * 100) / 100;//每月还款本息

            for (i = 1; i <= months; i++)
            {
                b = (amount * monthRate * (Math.Pow((1 + monthRate), months) - Math.Pow((1 + monthRate), (i - 1)))) / (Math.Pow((1 + monthRate), months) - 1);
                b = Math.Round(b * 100) / 100;
                c = a - b;
                c = Math.Round(c * 100) / 100;
                d = d - a;
                d = Math.Round(d * 100) / 100;
                if (i == months)
                {
                    c = c + d;
                    b = b - d;
                    c = Math.Round(c * 100) / 100;
                    b = Math.Round(b * 100) / 100;
                    d = 0;
                }

                var unit = new BackUnit();
                unit.Number = i;// 期数
                unit.TotalRate = totalRateIncome;// 总利息
                unit.TotalMoney = totalIncome;// 总还款
                unit.A = a;// 偿还本息  someNumber.ToString("N2");
                unit.B = b;// 偿还利息
                unit.C = c;// 偿还本金
                unit.D = d;// 剩余本金
                datalist.Add(unit);
            }

            return datalist;
        }
    }

    public class AnYueFuxiDaoqiHuanBen
    {
        /// <summary>
        /// 按月付息到期还本
        /// </summary>
        /// <param name="amount">投资金额</param>
        /// <param name="yearRate">年利率</param>
        /// <param name="months">投资期限,单位:月</param>
        /// <returns></returns>
        public static List<BackUnit> Compute(double amount, double yearRate, int months)
        {
            var datalist = new List<BackUnit>();  //new Array(Deadline);     //

            double rateIncome = amount * yearRate / 100 * (months / 12.0);
            double rateIncomeEve = (rateIncome / months);

            var total = amount + rateIncome;

            for (var i = 1; i < months; i++)
            {
                var unit = new BackUnit();
                unit.Number = i;// 期数
                unit.TotalRate = rateIncome;//Math.Round((Amount + TotalRate) * 100) / 100;// 总利息
                unit.TotalMoney = total;//TotalRate;// 总还款
                unit.A = rateIncomeEve;// 偿还本息  someNumber.ToString("N2");
                unit.B = rateIncomeEve;// 偿还利息
                unit.C = 0;// 偿还本金
                unit.D = amount * 1 + rateIncome * 1 - rateIncomeEve * i;// 剩余本金
                datalist.Add(unit);
            }

            datalist.Add(new BackUnit() {
                Number= months,
                TotalRate = rateIncome,
                TotalMoney = total,
                A = amount + rateIncomeEve,
                B = rateIncomeEve,
                C = amount,
                D = 0
            });
            return datalist;
        }
    }

    public class YicixingHuanBenFuxi
    {
        /// <summary>
        /// 一次性还本付息
        /// </summary>
        /// <param name="amount">投资金额</param>
        /// <param name="yearRate">年利率</param>
        /// <param name="months">投资期限,单位:月</param>
        /// <returns></returns>
        public static BackUnit Compute(double amount, double yearRate, int months)
        {
            BackUnit unit = new BackUnit();
            var rate = yearRate;
            var rateIncome = amount * rate / 100 * (months / 12.0);
            var totalIncome = amount + rateIncome;

            unit.Number = 1;// 期数
            unit.TotalRate = rateIncome;//Math.Round((Amount + TotalRate) * 100) / 100;// 总利息
            unit.TotalMoney = totalIncome;//TotalRate;// 总还款
            unit.A = totalIncome;// 偿还本息  someNumber.ToString("N2");
            unit.B = rateIncome;// 偿还利息
            unit.C = 0;// 偿还本金
            unit.D = 0;// 剩余本金
            return unit;
        }
    }

    public class BackUnit
    {
        //当前期数
        public int Number { get; set; }
        //总利息
        public double TotalRate { get; set; }
        //总还款
        public double TotalMoney { get; set; }
        //本期应还全部
        public double A { get; set; }
        //本期应还利息
        public double B { get; set; }
        //本期应还本金
        public double C { get; set; }
        //本期剩余本金
        public double D { get; set; }
    }

以下为运行结果:

image

再附送一个我所写的在线测试地址:http://p2p.tminji.com/phome/mockcompute。