Python程序,任何人都可以告诉我2这里有什么意义吗?

时间:2012-08-21 03:46:16

标签: python

在圆函数中,数字2表示第二个参数是什么意思?

##This is a program that calcs your credit card, and compounds down the total

a=float(raw_input("Enter the outstanding balance on your credit card:"))
b=float(raw_input("Enter the annual crdit card interest rate as a deicimal:"))           
c=float(raw_input("Enter the minimum monthly payment as a decimal:"))
for month in range(1, 13):
    print "Month: ", str(month)
    MMP = round((c * a),2)                  ##the 2 here and below, what does it do?
    print "Minimum monthly payment: ", MMP
    IP = round((b/12 * a),2)
    print "Interest payed: ", IP 
    PP = round(((c*a) - ((b/12)*a)),2)
    print "principal payed: ", PP
    a = round((a - PP),2)
    print "Remaining balance", a 

3 个答案:

答案 0 :(得分:4)

将2作为第二个参数传递给round,给出要舍入的小数位数。这会将它四舍五入到最近的浮点数,表示舍入到两位小数的数字。请注意,这是一个非常糟糕的主意,也是常见的错误来源。请使用fractions.Fraction或decimal.Decimal。

浮游物不应该用于赚钱,特别是当你像这样四舍五入时。

答案 1 :(得分:1)

2是在舍入操作中使用的小数位数。看看这里:http://docs.python.org/library/functions.html#round

答案 2 :(得分:1)

这会舍入一个数字,以便在操作后它有2位十进制数字。 查看以下文档:

http://docs.python.org/library/functions.html#round