2017-09-07 12 views
0

私はちょうどJavaのバックグラウンドからPythonを学び始めました。なぜ私の数字がmonthlyPaymentの下で丸まっていないのか理解できていません。毎月の支払い額を100分の1に丸めた問題

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) + "$.") 

私のエラーがどこで発生したのか理解していただけたら助かります。

+2

'int(monthlyPayment)'は、あなたの数値を整数に変換します。どのように整数を丸めると思いますか? – DyZ

+0

'float'を' int'にしたくない – Milk

+0

'monthlyPayment = int(round(monthlyPayment/100.0,2)* 100)'私は毎月の支払いを最も近い100にしたいと思っています。 – kaza

答えて

0

int関数を使用して変数を整数に変換すると、存在する小数点が削除されます。整数を丸めるのは役に立たない。

int(10.33344443) 
>> 10 

あなたがintを削除する場合は、それが期待するようにスクリプトが動作しますので、変数は、小数点precentでfloat残ります。

round(10.33344443,2) 
>> 10.33 
+0

ありがとう!私はこれを完全に見落とした! –

関連する問題