The problem is not the rounding, but the interpretation of /
.
In python 2 if dividing 2 integers with /
you get an integer not a float.
In python 3 you get a float if you use /
- the "pure" integer division is done by using //
.
Python 2:
print(100/60) # ==> 1
print(100/60.0) # ==> 1.6666666666...
Python 3:
print (100/60) # ==> 1.6666
print (100//60) # ==> 1
They both get rounded accordingly, but if you input a 1 into round, it will result in 1.
You can read more about the reason for the changed behaviour here: PEP 238
The current division (/
) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.
We propose to fix this by introducing different operators for different operations: x/y
to return a reasonable approximation of the mathematical result of the division ("true division"), x//y
to return the floor ("floor division"). We call the current, mixed meaning of x/y
"classic division".