1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | #coding:utf-8 import sys from prettytable import PrettyTable ''' 月利率 = 年利率/12 1.等额本息计算公式:每月还款金额 =〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数 - 1〕 每月利息 = 每月还款金额 - (1 + 月利率 )^(还款期数-1) * (每月还款金额 - 月利率*贷款本金) 每月偿还本金 = ( 1 + 月利率)^(还款期数-1) * (每月还款金额 - 月利率*贷款本金) 2.等额本金计算公式:每月还款金额 =(贷款本金 / 还款月数)+(本金 — 已归还本金累计额)* 每月利率 每月偿还本金=贷款本金/还款月数 每月利息=(本金-累计已还本金)×月利率 ''' YEAR_LPR = 0.0385 #年利率 MONTH_LPR = round (YEAR_LPR / 12 , 12 ) #月利率 UNIT = 10000 #单位万元 ''' 月供计算 periods:第n期还款 pay_month:总还款月数 total:单位(万元) 等额本息:pay_way=1 等额本金:pay_way=2 ''' #月供 def get_month_pay(total,periods = 1 ,pay_month = 360 ,pay_way = 1 ): result = 0 if pay_way = = 1 : result = (total * UNIT * MONTH_LPR * ((MONTH_LPR + 1 ) * * pay_month)) / ((MONTH_LPR + 1 ) * * pay_month - 1 ) elif pay_way = = 2 : result = (total * UNIT / pay_month) + (total * UNIT - (total * UNIT / pay_month) * (periods - 1 ) ) * MONTH_LPR return round (result, 6 ) # 每月还款利息 def get_month_interest(total,periods = 1 ,pay_month = 360 ,pay_way = 1 ): result = 0 if pay_way = = 1 : month_pay = round ((total * UNIT * MONTH_LPR * ((MONTH_LPR + 1 ) * * pay_month)) / ((MONTH_LPR + 1 ) * * pay_month - 1 ), 6 ) #每月还款金额 result = month_pay - (( 1 + MONTH_LPR) * * (periods - 1 )) * (month_pay - MONTH_LPR * total * UNIT) elif pay_way = = 2 : result = (total * UNIT - (total * UNIT / pay_month) * (periods - 1 ) ) * MONTH_LPR return round (result, 6 ) #每月还款本金 def get_month_principal(total,periods = 1 ,pay_month = 360 ,pay_way = 1 ): result = 0 if pay_way = = 1 : month_pay = round ((total * UNIT * MONTH_LPR * ((MONTH_LPR + 1 ) * * pay_month)) / ((MONTH_LPR + 1 ) * * pay_month - 1 ), 6 ) #每月还款金额 result = (( 1 + MONTH_LPR) * * (periods - 1 )) * (month_pay - MONTH_LPR * total * UNIT) elif pay_way = = 2 : result = total * UNIT / pay_month return round (result, 6 ) def main(total,pay_month): tb = PrettyTable([ "期数" , "还款金额" , "还款本金" , "还款利息" , "总还款数" , "总还款本金" , "剩余还款本金" , "总还款利息" ]) sum_month_pay1 = 0 sum_month_pay2 = 0 sum_month_principal1 = 0 sum_month_principal2 = 0 sum_month_interest1 = 0 sum_month_interest2 = 0 for periods in range ( 1 ,pay_month + 1 ): month_pay1 = get_month_pay(total,periods,pay_month,pay_way = 1 ) #月供 month_pay2 = get_month_pay(total,periods,pay_month,pay_way = 2 ) month_interest1 = get_month_interest(total,periods,pay_month,pay_way = 1 ) #利息 month_interest2 = get_month_interest(total,periods,pay_month,pay_way = 2 ) month_principal1 = get_month_principal(total,periods,pay_month,pay_way = 1 ) #本金 month_principal2 = get_month_principal(total,periods,pay_month,pay_way = 2 ) sum_month_pay1 + = month_pay1 #总还款数 sum_month_pay2 + = month_pay2 sum_month_principal1 + = month_principal1 #总还款本金 sum_month_principal2 + = month_principal2 sum_month_interest1 + = month_interest1 #总还款利息 sum_month_interest2 + = month_interest2 row1 = [ '等额本息-{}' . format (periods), round (month_pay1, 2 ), round (month_principal1, 2 ), round (month_interest1, 2 ), round (sum_month_pay1, 2 ), round (sum_month_principal1, 2 ),<br> round (total * UNIT - sum_month_principal1, 2 ), round (sum_month_interest1, 2 )] row2 = [ '等额本金-{}' . format (periods), round (month_pay2, 2 ), round (month_principal2, 2 ), round (month_interest2, 2 ), round (sum_month_pay2, 2 ), round (sum_month_principal2, 2 ),<br> round (total * UNIT - sum_month_principal2, 2 ), round (sum_month_interest2, 2 )] tb.add_row(row1) tb.add_row(row2) # if periods>24:break print (tb) if __name__ = = "__main__" : main( 45 , 360 ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
2023-03-21 mtail 日志采集