Python 整数转百分比
我需要把每月付款转换成百分比,所以我想把范围从 [4, 5, 6, 7, 8] 改成 [0.04, 0.05, 0.06, 0.07, 0.08]。
你知道怎么做吗?这样还能从总付款中计算出来。
import math
loanAmt=int(input("Enter the Amount (greater then 0) of the Loan: "))
numYears=int(input("Enter the number of years as an integer: "))
for monthlyRate in range(4,9):
monthlyPayment = loanAmt * monthlyRate / (1 - math.pow(1 / (1 + monthlyRate), numYears * 12))
totalPayment = monthlyPayment * numYears * 12
print("{0:.0f}%".format(monthlyRate),'\t','$%.2f' %monthlyPayment,'\t','\t','$%.2f' %totalPayment)
2 个回答
0
或者你也可以使用一种叫做 列表推导式 的写法。
percent_rate = [rate/100.0 for rate in monthly_rate]
# where monthly_rate is the list of all ints like [4, 5, 6, 7, 8]
2
你可以使用这个:
for monthlyRate in (x/100.0 for x in range(4,9)):
print monthlyRate
0.04
0.05
0.06
0.07
0.08