抵押贷款计算器数学错误
Mortgage calculator math error
此程序运行 正常,但每月付款 returns 完全关闭。对于 400,000 美元的本金、11% 的利率和 10 年的付款期,它 returns 每月支付 44000.16 美元。我用谷歌搜索了抵押贷款支付的等式(算法?)并将其输入,但不确定我哪里出错了。
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
print "Your monthly payment would be", locale.currency(mortgage(principal, interest, n))
问题出在您使用的利率上。您要求的是年利率,从不转换为月利率。
来自https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula:
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.
我刚刚在我的电脑上试了一下,用利率除以 12 计算得出每月 5510 美元,这与其他抵押贷款计算器一致。
此程序运行 正常,但每月付款 returns 完全关闭。对于 400,000 美元的本金、11% 的利率和 10 年的付款期,它 returns 每月支付 44000.16 美元。我用谷歌搜索了抵押贷款支付的等式(算法?)并将其输入,但不确定我哪里出错了。
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
print "Your monthly payment would be", locale.currency(mortgage(principal, interest, n))
问题出在您使用的利率上。您要求的是年利率,从不转换为月利率。
来自https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula:
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.
我刚刚在我的电脑上试了一下,用利率除以 12 计算得出每月 5510 美元,这与其他抵押贷款计算器一致。