標籤:
你若懂行,那便有用,如下:
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; }
}
以下為運行結果:
再附送一個我所寫的線上測試地址:http://p2p.tminji.com/phome/mockcompute。
C#實現的等額本息法、按月付息到期還本法、一次性還本付息法