python计算本息总和_[Python]等额本息房贷计算器

等额本息还款法:

每月月供额=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕

每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕

每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕

总利息=还款月数×每月月供额-贷款本金

比如贷款200万,年贷款利率4.9%,贷款期限30年

根据等额本息,算出每个月还贷10614.53,贷款200万,还了182.12万的利息

第一个月还的10614.53中,8166.67还的是利息,2447.87还的是本金

第二月还了8156.67的利息,2457.86的本金

以此类推

运行结果

1285873-20180316145404973-141928811.png

代码如下

defmonthlyPayment(principal, year_rate, year_duration):

monthly_rate= year_rate / (12 * 100) #convert 4.9 to 0.049 and monthly interest rate

month_amounts = year_duration * 12

#每月月供

monthly_payment = (principal * monthly_rate * (1 + monthly_rate) ** month_amounts) /(

(1 + monthly_rate) ** month_amounts - 1)#总利息

total_interest_payable = monthly_payment * month_amounts -principalprint('-----------------------------------')print ('Total interest payable is %.2f' %total_interest_payable)for i in range (1, month_amounts + 1):#每月应还利息

monthly_interest_payable = principal * monthly_rate * ((1 + monthly_rate) ** month_amounts - (1 + monthly_rate) ** (i - 1 ))/ ((1 + monthly_rate) ** month_amounts -1)#每月应还本金

monthly_principal_payable = principal * monthly_rate * (1 + monthly_rate) ** (i - 1)/ ((1 + monthly_rate) ** month_amounts -1)#每月利息占比

monthly_interest_percentage = monthly_interest_payable * 100 /monthly_paymentprint('-----------------------------------')print ('%dth monthly payment is : %.2f (Interest: %.2f and Principal: %.2f)' %(i, monthly_payment,monthly_interest_payable,monthly_principal_payable))print('%dth month interest percentage is %.2f %%' %(i,monthly_interest_percentage))return

if __name__ == '__main__':

principal= int(input('Please input your loan amounts:'))

year_rate= float(input('Please input Year Debt Interest Rate:(such as 4.9,it means 4.9%)'))

year_duration= int(input('Please input Debt Year Duration:'))

monthlyPayment(principal, year_rate, year_duration)

根据等额本息法公式可以计算出每月还款,具体每月还款里面,多少是利息,多少是本金,除了用之前的公式,还可以用下面的方法考虑

还是用上面的例子 ,贷款200万,年贷款利率4.9%,贷款期限30年,等额本息每个月需还贷10614.53

第一个月房贷里还的利息是2,000,000×(4.9%/12)= 8166.67

得出第一个房贷还的本金是 10614.53 - 8166.67 = 2447.87

剩余总本金为2,000,000-2447.87 = 1,997,552.13

---------------------------------------------------------------------

第二个月房贷需要还的利息为:剩余总本金×月利息:1,997,552.13 ×(4.9%/12) = 8156.67

第二个月房贷还的本金是10614.53 - 8156.67 = 2457.86

剩余总本金为1,997,552.131-2457.87 = 1,995,094.26

----------------------------------------------------------------------

第三个月房贷需要还的利息为:为剩余总本金×月利息:1,995,094.26 ×(4.9%/12) = 8146.63

第三个月房贷还的本金是10614.53 - 8146.63 = 2467.89

剩余总本金为1,995,094.26-2467.89= 1,992,626.37

以此类推

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值