[Python]等額本息房貸計算器

等額本息還款法:spa

每個月月供額=〔貸款本金×月利率×(1+月利率)^還款月數〕÷〔(1+月利率)^還款月數-1〕
每個月應還利息=貸款本金×月利率×〔(1+月利率)^還款月數-(1+月利率)^(還款月序號-1)〕÷〔(1+月利率)^還款月數-1〕
每個月應還本金=貸款本金×月利率×(1+月利率)^(還款月序號-1)÷〔(1+月利率)^還款月數-1〕
總利息=還款月數×每個月月供額-貸款本金rest

 

好比貸款200萬,年貸款利率4.9%,貸款期限30年code

根據等額本息,算出每一個月還貸10614.53,貸款200萬,還了182.12萬的利息blog

第一個月還的10614.53中,8166.67還的是利息,2447.87還的是本金ip

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

以此類推input

運行結果it

 

代碼以下io

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)

 

 

根據等額本息法公式能夠計算出每個月還款,具體每個月還款裏面,多少是利息,多少是本金,除了用以前的公式,還能夠用下面的方法考慮class

 

仍是用上面的例子 ,貸款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

 

以此類推

相關文章
相關標籤/搜索