Python等额本息算法
@staticmethod
def gen_repay_plan(principle, year_rate, period):
monthly_rate = year_rate / 100 / 12
print("-----等额本息计算-----")
b = principle * monthly_rate * pow((1 + monthly_rate), period) / (pow((1 + monthly_rate), period) - 1)
Y = period * principle * monthly_rate * pow((1 + monthly_rate), period) / (pow((1 + monthly_rate), period) - 1) - principle
c1 = principle * monthly_rate
e1 = Y - c1
a1 = principle - (b - c1)
print("第1个月应还利息为%s,应还本金为%s,还款总额为%s,剩余本金为%s" % (round(c1, 2), round(b - c1, 2), round(b, 2), round(a1, 2)))
start_date = datetime.datetime.now().strftime("%Y-%m-%d") + " 00:00:00"
if int(datetime.datetime.now().strftime("%d")) > 28:
repay_date = (datetime.datetime.now() + relativedelta(months=1)).strftime("%Y-%m") + " -01 00:00:00"
else:
repay_date = (datetime.datetime.now() + relativedelta(months=1)).strftime("%Y-%m-%d") + " 00:00:00"
grance_date = datetime.datetime.strptime(repay_date, "%Y-%m-%d %H:%M:%S") + relativedelta(days=3)
print("start_date: " + str(start_date) + " repay_date: " + str(repay_date) + " grance_date: " + str(grance_date))
if period > 1:
for t in range(2, period + 1):
start_date = str(repay_date)
repay_date = str(datetime.datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S") + relativedelta(months=1))
grance_date = str(datetime.datetime.strptime(repay_date, "%Y-%m-%d %H:%M:%S") + relativedelta(days=3))
ci = (principle * monthly_rate - b) * pow((1 + monthly_rate), (t - 1)) + b
bi = b - ci
a1 = round(a1 - bi, 2)
print("第%d个月应还利息为%s,应还本金为%s,还款总额为%s,剩余本金为%s" % (round(t, 2), round(ci, 2), round(bi, 2), round(b, 2), abs(a1)))
print("start_date: " + str(start_date) + " repay_date: " + str(repay_date) + " grance_date: " + str(grance_date))
日期转换
repay_date = str(datetime.datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S") + relativedelta(months=1))
grance_date = str(datetime.datetime.strptime(repay_date, "%Y-%m-%d %H:%M:%S") + relativedelta(days=3))
测试结果
第1个月应还利息为40.0,应还本金为149.12,还款总额为189.12,剩余本金为1850.88
start_date: 2020-04-09 00:00:00 repay_date: 2020-05-09 00:00:00 grance_date: 2020-05-12 00:00:00
第2个月应还利息为37.02,应还本金为152.1,还款总额为189.12,剩余本金为1698.78
start_date: 2020-05-09 00:00:00 repay_date: 2020-06-09 00:00:00 grance_date: 2020-06-12 00:00:00
第3个月应还利息为33.98,应还本金为155.14,还款总额为189.12,剩余本金为1543.64
start_date: 2020-06-09 00:00:00 repay_date: 2020-07-09 00:00:00 grance_date: 2020-07-12 00:00:00
第4个月应还利息为30.87,应还本金为158.25,还款总额为189.12,剩余本金为1385.39
start_date: 2020-07-09 00:00:00 repay_date: 2020-08-09 00:00:00 grance_date: 2020-08-12 00:00:00
第5个月应还利息为27.71,应还本金为161.41,还款总额为189.12,剩余本金为1223.98
start_date: 2020-08-09 00:00:00 repay_date: 2020-09-09 00:00:00 grance_date: 2020-09-12 00:00:00
第6个月应还利息为24.48,应还本金为164.64,还款总额为189.12,剩余本金为1059.34
start_date: 2020-09-09 00:00:00 repay_date: 2020-10-09 00:00:00 grance_date: 2020-10-12 00:00:00
第7个月应还利息为21.19,应还本金为167.93,还款总额为189.12,剩余本金为891.41
start_date: 2020-10-09 00:00:00 repay_date: 2020-11-09 00:00:00 grance_date: 2020-11-12 00:00:00
第8个月应还利息为17.83,应还本金为171.29,还款总额为189.12,剩余本金为720.12
start_date: 2020-11-09 00:00:00 repay_date: 2020-12-09 00:00:00 grance_date: 2020-12-12 00:00:00
第9个月应还利息为14.4,应还本金为174.72,还款总额为189.12,剩余本金为545.4
start_date: 2020-12-09 00:00:00 repay_date: 2021-01-09 00:00:00 grance_date: 2021-01-12 00:00:00
第10个月应还利息为10.91,应还本金为178.21,还款总额为189.12,剩余本金为367.19
start_date: 2021-01-09 00:00:00 repay_date: 2021-02-09 00:00:00 grance_date: 2021-02-12 00:00:00
第11个月应还利息为7.34,应还本金为181.78,还款总额为189.12,剩余本金为185.41
start_date: 2021-02-09 00:00:00 repay_date: 2021-03-09 00:00:00 grance_date: 2021-03-12 00:00:00
第12个月应还利息为3.71,应还本金为185.41,还款总额为189.12,剩余本金为0.0
start_date: 2021-03-09 00:00:00 repay_date: 2021-04-09 00:00:00 grance_date: 2021-04-12 00:00:00
blog.csdn.net/weixin_3060…
stackabuse.com/converting-…