-
Notifications
You must be signed in to change notification settings - Fork 9
/
mortgage_calculator.py
59 lines (44 loc) · 2.16 KB
/
mortgage_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
def calculate_mortgage(mortgage_details):
"""
Calculates mortgage monthly payment rate.
Tests:
>>> calculate_mortgage({'price': '$1,000,000', 'rate': '5.25', 'downpayment': '200000', 'loan': '30'})
('$4,418', '$1,590,347')
>>> calculate_mortgage({'price': '$650,000', 'rate': '3.83', 'downpayment': '169000', 'loan': '20'})
('$2,872', '$689,246')
>>> calculate_mortgage({'price': '$240,000', 'rate': '1.12', 'downpayment': '240000', 'loan': '15'})
('$0', '$0')
"""
MONTHS_IN_YEAR = 12
PERCENT_CONVERSION = 100
# Get price, mortgage rate, downpayment amount
price = int(mortgage_details['price'])
rate = ((float(mortgage_details['rate'])/PERCENT_CONVERSION)/MONTHS_IN_YEAR)
downpayment = int(re.sub('[^\d.]+', '', mortgage_details['downpayment']))
hoa = mortgage_details.get('hoa')
# If HOA exists, turn it into an integer, otherwise it is zero
try:
hoa = int(mortgage_details['hoa'])
except ValueError:
hoa = 0
# Translate loan term in years to months
loan = mortgage_details['loan']
# Total loan payments
loan_payments = int(loan[0:2]) * MONTHS_IN_YEAR
# Calculate monthly payment
if rate == 0:
monthly_payment = float(price)/loan_payments
else:
monthly_payment = (price - downpayment) * (rate * (1 + rate) ** loan_payments) / ((1 + rate) ** loan_payments - 1)
# Calculate total monthly payment with HOA fees if one exists
monthly_plus_hoa_payment = monthly_payment + hoa
formatted_monthly_plus_hoa_payment = '${:,}'.format(int(round(monthly_plus_hoa_payment)))
# Calculate total interest paid in span of loan
total_interest_paid = monthly_payment * loan_payments - price
formatted_total_interest_paid = '${:,}'.format(int(round(monthly_payment * loan_payments - price)))
# Calculate total HOA fees paid in span of loan
total_hoa_paid = hoa * loan_payments
# Calculate the total mortgage paid with interest
total_mortgage_payment = '${:,}'.format(int(round(price + total_interest_paid + total_hoa_paid)))
return (formatted_monthly_plus_hoa_payment, total_mortgage_payment)