[英]Python program, can anyone tell me what significance of the 2 is here?
In the round function, what means the number 2 as the second argument? 在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
The 2 is passed as the second argument to round, giving the number of decimal places to round to. 2作为舍入的第二个参数传递,给出舍入到的小数位数。 This will round it to the nearest float representing the number rounded to two decimal places. 这会将其舍入到最接近的浮点数,该数字代表四舍五入到小数点后两位的数字。 Note that this is a very bad idea and a common source of bugs. 请注意,这是一个非常糟糕的主意,并且是错误的常见来源。 Use a fractions.Fraction or decimal.Decimal instead. 请使用分数,分数或十进制。十进制代替。
Floats should never be used for money, especially when you're rounding them like this. 浮点数绝不能用来赚钱,尤其是当您像这样对它们进行四舍五入时。
2
is the quantity of decimal digits to use in the rounding operation. 2
是舍入运算中使用的小数位数。 Take a look here: http://docs.python.org/library/functions.html#round 在这里看看: http : //docs.python.org/library/functions.html#round
This rounds a number so that after the operation it has 2 decimal digits. 这将舍入一个数字,以便在操作后它具有2个十进制数字。 Check the docs below: 检查以下文档:
http://docs.python.org/library/functions.html#round http://docs.python.org/library/functions.html#round
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.