Monthly payment, total interest and the effect of an extra monthly payment. Change the inputs, results update.
# Mortgage Calculator
# Monthly payment, total interest and the effect of paying a bit more every month.
# Change the inputs, every result below updates by itself.
# Inputs
homePrice = 350000 €
downPayment = 20 %
annualInterestRate = 3.8 %
loanTerm = 25 years
extraPerMonth = 150 €
# Loan
loanAmount = homePrice * (1 - downPayment)
# Monthly rate and number of payments
r = annualInterestRate / 12
n = loanTerm / (1 month)
# Standard annuity (amortization) formula, same as used by banks for fixed-rate loans: P = L * r * (1+r)^n / ((1+r)^n - 1)
monthlyPayment = loanAmount * (r * (1 + r)^n) / ((1 + r)^n - 1)
# Over the whole term
totalPaid = monthlyPayment * n
totalInterest = totalPaid - loanAmount
interestShare = totalInterest / totalPaid
# With an extra payment every month
# The balance after k payments is B = L*(1+r)^k - p*((1+r)^k - 1)/r
p = monthlyPayment + extraPerMonth
# Number of payments until the balance reaches zero
nWithExtra = log(p / (p - loanAmount * r)) / log(1 + r)
yearsWithExtra = nWithExtra * 1 month to years
interestWithExtra = p * nWithExtra - loanAmount
# What the extra payment saves
interestSaved = totalInterest - interestWithExtra
timeSaved = loanTerm - yearsWithExtra