简体   繁体   English

Python程序,谁能告诉我2在这里的含义是什么?

[英]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.

相关问题 谁能告诉我这里的代码有什么问题? - Can anyone tell me what's wrong with my code here? 谁能告诉我这里指的是什么“节点”以及示例测试用例是如何工作的? 数组中边的确切含义是什么? - Can anyone tell me what “node” is being referred to here & how is the sample test case even working? What is the exact meaning of edges in the array? 谁能告诉我Python中的合并排序有什么问题吗? - Can anyone tell me what's wrong with my Merge Sort in Python? 任何人都可以告诉我python pyodbc中connection.commit()的意义是什么? - Can anyone tell me what' s the point of connection.commit() in python pyodbc ? 谁能告诉我我做错了什么,字典与 Python 中的列表? - Can anyone tell me what I am doing wrong, dictionary vs list in Python? 谁能告诉我这个列表理解有什么问题? - Can anyone tell me what is wrong with this list comprehension? 谁能告诉我这个 CNN 代码中的错误是什么? - Can anyone tell me what is the error in this CNN code? 谁能告诉我我的功能出了什么问题? - Can anyone tell me what's wrong with my function? 谁能告诉我我的 if 条件有什么问题? - can anyone tell me what is wrong with my if condition? 代码跳过 Python 中的命令。 谁能告诉我为什么? - The code skips a command in Python. Can anyone tell me why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM