将每月付款四舍五入至百分之一的问题

时间:2017-09-07 01:39:58

标签: python

我刚刚开始学习python,从java背景切换,我无法理解为什么我的数字不会在monthlyPayment下转。

loan = input("Please enter the loan amount.\n")
loan = int(loan)
interest = input("Please enter the interest rate.\n")
interest = int(interest)
years = input("Please enter the number of years desired.\n")
years = int(years)
i = interest/1200
monthlyPayment = (i/(1-(1+i)**(-12*years))*loan)
monthlyPayment = round(int(monthlyPayment),2)
print("With a loan of " + str(loan) + "$, an interest rate of " + 
str(interest) + "%, and after " + str(years) + " years the monthly payment 
totals at " + str(monthlyPayment) + "$.")

任何帮助了解我的错误发生的位置将不胜感激!

1 个答案:

答案 0 :(得分:0)

通过使用int函数将变量转换为整数,可以删除任何小数点。对整数进行舍入是没用的。

e.g。

int(10.33344443)
>> 10

如果您删除int变量,则保留float小数点前缀,以便您的脚本按预期运行。

e.g。

round(10.33344443,2)
>> 10.33