Mortgage calculator math error

conjenks Source

This program runs fine, but the monthly payment it returns is totally off. For a principal amount of $400,000, interest rate of 11%, and a 10-year payment period, it returns the monthly payment of $44000.16. I googled the equation (algorithm?) for mortgage payments and put it in, not sure where I'm going wrong.

import locale
locale.setlocale(locale.LC_ALL, '')

def mortgage(principal, interest, n):
    payment = principal*((interest*(1+interest)**n) / ((1+interest)**n-1))
    return payment

principal = float(input("What is the amount of the loan you are taking out? $"))
interest = float(input("What is the interest rate? (%) ")) / 100
n = float(input("How many years? ")) * 12
print "Your monthly payment would be", locale.currency(mortgage(principal, interest, n))


answered 3 years ago Ron #1

The problem is in your interest rate used. You request the annual interest rate and never convert to a monthly interest rate.


r - the monthly interest rate, expressed as a decimal, not a percentage. Since the quoted yearly percentage rate is not a compounded rate, the monthly percentage rate is simply the yearly percentage rate divided by 12; dividing the monthly percentage rate by 100 gives r, the monthly rate expressed as a decimal.

I just tried this on my computer and dividing the interest rate by 12 calculated $5510/month which agrees with other mortgage calculators.

comments powered by Disqus