简体   繁体   中英

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?

##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. 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. Take a look here: http://docs.python.org/library/functions.html#round

This rounds a number so that after the operation it has 2 decimal digits. Check the docs below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM