0

在 round 函数中,作为第二个参数的数字 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 
4

3 回答 3

4

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

浮点数永远不应该用来赚钱,尤其是当你像这样四舍五入时。

于 2012-08-21T03:49:26.287 回答
1

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

于 2012-08-21T03:48:26.090 回答
1

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

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

于 2012-08-21T03:49:38.457 回答