等额本金VS等额本息计算器来了

最近大家都在讨论存量房贷下调利率的事情,今天我们来算一下房贷利率下调之后能省下多少钱

一、贷款计算器-等额本金VS等额本息

#贷款计算器
class LoanCalculator:
    #等额本金还款方式
    def average_capital(self,loan_amount,loan_years,rate_year):
        """
        每月还款金额 = (借款金额/期数)+(借款本金-累计已还款金额)*月利率
        :param loan_amount: 贷款金额
        :param loan_years: 贷款年限
        :param rate_year: 贷款年利率
        :return:
        """
        # 总利息
        interest = 0
        # 月利率
        month_rate = rate_year/100 / 12
        # 每月应还本金
        principal = loan_amount / loan_years / 12
        for i in range(0,loan_years*12):
            if i == 0:
                month_amount = principal + (loan_amount * month_rate)
                print(f'第{i+1}期还款金额:',month_amount)
            else:
                month_amount = principal +(loan_amount * month_rate)
                print(f'第{i+1}期还款金额:',month_amount)
            interest += loan_amount * month_rate
            #还款本金每个月递减
            loan_amount -= principal
        print("总利息",interest)

    #等额本息还款方式
    def equality_corp(self,loan_amount,loan_years,rate_year):
        """
        月利息 = 贷款金额 * 月利率
        每月应还金额 = 贷款金额 * (月利率 * (1 + 月利率) ** 贷款月数) / ((1 + 月利率) ** 贷款月数 - 1)
        :param loan_amount: 贷款本金
        :param loan_years: 贷款月数
        :param rate_year: 贷款年利率
        :return:
        """
        #还款总利息
        total_interest = 0
        month_rate = rate_year/100/12 #月利率
        loan_month  =  loan_years * 12 #还款期数
        #每月还款金额
        amount = loan_amount * (month_rate * (1 + month_rate) ** loan_month) / ((1 + month_rate) ** loan_month - 1)
        print('等额本息每月还款金额:',amount)
        # 每月利息计算
        for i in range(0,loan_month):
            #每月还款利息
            interest = loan_amount * month_rate
            print(f'第{i+1}期还款利息:', interest)
            #累加还款利息
            total_interest += interest
            #每月还款本金
            principal = amount - interest
            print(f'第{i+1}期还款本金:',principal)
            #还款本金每月递减
            loan_amount -= principal
        print("等额本息还款总利息:",total_interest)
if __name__ == '__main__':
    cl = LoanCalculator()
    cl.average_capital(loan_amount=500000,loan_years=20,rate_year=5.9)
    cl.equality_corp(loan_amount=500000,loan_years=20,rate_year=5.9)

你可能感兴趣的:(python,开发语言)