P2P金融利息和复利算法

// 计算投资金额amt,投资几个月mons所有收益算法
function calcProfit(amt, mons) {
	var totalRate = 0;
	var yearDays = 365;
	var monthDays = yearDays / 12;
	for (var i = 1; i <= mons; i++) {
		var num = i < 6 ? i : 6;
		var rate = (0.045 + ((num - 1) * 0.005)) * 100 / 100;
		totalRate += rate * 10e10 / 10e10;
	}
	return amt * (totalRate / yearDays * monthDays);
}
// 计算复利利息
function calcFuLiProfit(amt, mons) {
	var yearDays = 365;
	var a = function(rate) {
		return rate / yearDays * amt;
	};
	var b = function(rate, days) {
		if (days > 1) {
			return a(rate) + b(rate, days - 1);
		} else {
			return a(rate);
		}
	};
	var totalAmt = 0;
	var monthDays = yearDays / 12;
	for (var i = 1; i <= mons; i++) {
		var num = i < 6 ? i : 6;
		var rate = (0.045 + ((num - 1) * 0.005)) * 100 / 100;
		totalAmt += b(rate, monthDays);
	}
	return totalAmt;
}

注:本算法仅供参考,算法涉及数值请以实际计算规则进行修改。

相关推荐