def calculate_equal_principal(principal, annual_rate, months):
"""
计算等额本金还款计划的各项数据
参数:
principal (float): 贷款本金
annual_rate (float): 年利率(百分比,例如5%)
months (int): 贷款期限(月)
返回:
dict: 包含首月月供、每月递减金额、总利息、总还款额及详细还款计划的字典
"""
monthly_rate = annual_rate / 100 / 12
monthly_principal = principal / months
total_interest = 0
remaining = principal
schedule = []
for month in range(1, months + 1):
current_principal = remaining if month == months else monthly_principal
interest = remaining * monthly_rate
monthly_payment = current_principal + interest
total_interest += interest
remaining -= current_principal
schedule.append({
'期数': month,
'月供': round(monthly_payment, 2),
'本金': round(current_principal, 2),
'利息': round(interest, 2),
'剩余本金': abs(round(remaining, 2))
})
monthly_decrease = round(monthly_principal * monthly_rate, 2)
return {
'首月月供': round(schedule[0]['月供'], 2),
'每月递减': monthly_decrease,
'总利息': round(total_interest, 2),
'总还款': round(principal + total_interest, 2),
'还款计划': schedule
}
money = int(input("请输入本金:"))
months = int(input("请输入总期数:"))
rate = float(input("请输入年利率:"))
print(f"等额本金,本金: {money}, 总期数{months}, 年利率{rate}")
loan = calculate_equal_principal(money, rate, months)
print(f"首月月供: {loan['首月月供']}")
print(f"每月递减: {loan['每月递减']}")
print(f"总利息: {loan['总利息']}")
print(f"总还款: {loan['总还款']}")
print("\n")
previous_months = 12
total_interest = 0
print(f"前{previous_months}个月还款计划:")
for entry in loan['还款计划'][:previous_months]:
total_interest += entry['利息'] * 100
print(
f"期数{entry['期数']}: 月供{entry['月供']}元 = 本金{entry['本金']}元 + 利息{entry['利息']}元,剩余本金{entry['剩余本金']}元")
print(f"前{previous_months}个月的总利息", total_interest / 100)
结果
请输入本金:100000
请输入总期数:60
请输入年利率:4.8
等额本金,本金: 100000, 总期数60, 年利率4.8
首月月供: 2066.67
每月递减: 6.67
总利息: 12200.0
总还款: 112200.0
前12个月还款计划:
期数1: 月供2066.67元 = 本金1666.67元 + 利息400.0元,剩余本金98333.33元
期数2: 月供2060.0元 = 本金1666.67元 + 利息393.33元,剩余本金96666.67元
期数3: 月供2053.33元 = 本金1666.67元 + 利息386.67元,剩余本金95000.0元
期数4: 月供2046.67元 = 本金1666.67元 + 利息380.0元,剩余本金93333.33元
期数5: 月供2040.0元 = 本金1666.67元 + 利息373.33元,剩余本金91666.67元
期数6: 月供2033.33元 = 本金1666.67元 + 利息366.67元,剩余本金90000.0元
期数7: 月供2026.67元 = 本金1666.67元 + 利息360.0元,剩余本金88333.33元
期数8: 月供2020.0元 = 本金1666.67元 + 利息353.33元,剩余本金86666.67元
期数9: 月供2013.33元 = 本金1666.67元 + 利息346.67元,剩余本金85000.0元
期数10: 月供2006.67元 = 本金1666.67元 + 利息340.0元,剩余本金83333.33元
期数11: 月供2000.0元 = 本金1666.67元 + 利息333.33元,剩余本金81666.67元
期数12: 月供1993.33元 = 本金1666.67元 + 利息326.67元,剩余本金80000.0元
前12个月的总利息 4360.0