python公积金贷款计算器代码
时间: 2024-10-09 07:03:35 浏览: 42
python tkinter实现 贷款计算器
Python公积金贷款计算器通常用于帮助用户计算公积金贷款的月供、总利息等信息。以下是一个简单的示例代码,假设公积金贷款的利率固定,贷款年限和每月还款额可以输入:
```python
class LoanCalculator:
def __init__(self, loan_amount, annual_interest_rate, loan_term_years):
self.loan_amount = loan_amount
self.annual_interest_rate = annual_interest_rate / 100 # 将百分比转换为小数
self.loan_term_years = loan_term_years
self.months_in_year = 12
def calculate_monthly_payment(self):
monthly_interest_rate = self.annual_interest_rate / self.months_in_year
monthly_payment = (self.loan_amount * monthly_interest_rate) / \
(1 - (1 + monthly_interest_rate) ** (-self.months_in_year * self.loan_term_years))
return round(monthly_payment, 2)
def total_interest_paid(self):
months_of_payment = self.loan_term_years * self.months_in_year
total_interest = self.calculate_monthly_payment() * months_of_payment - self.loan_amount
return round(total_interest, 2)
# 使用示例
loan = LoanCalculator(100000, 3.5, 20)
monthly_payment = loan.calculate_monthly_payment()
total_interest = loan.total_interest_paid()
print(f"每月还款额: {monthly_payment}元")
print(f"总利息支付: {total_interest}元")
阅读全文