[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的本金

以此類推

運行結果

 

代碼如下

def monthlyPayment(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 - principal
    print('-----------------------------------')
    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_payment

        print('-----------------------------------')
        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

 

以此類推


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM