0
round() function in Python3
Why print(round(2, 2)) prints 2 instead of 2.00? Thx!
5 Respostas
+ 5
I think it is not working because you are trying to round an integer. There is no need to round an integer.
print(round(2,2)) gives 2
but print(round(2.00,2)) gives 2.0
and print(round(2.739029271,3)) gives 2.739.
So, you should use a float instead of int as there is no point in rounding an int. And if there is still nothing to round in the float, it will not show many zeroes.
For Example--
print(round(2.0,5)) will still give 2.0 not 2.00000
Similarly if there are only zeroes in the part you want to round and other numbers after that, it will show--
For Example--
print(round(2.000001, 5))
Then it will not show 2.00000 but only 2.0
print(round(2.00001,5))
It will show all 0s as the fifth number after the decimal point is not 0.
2.00001 will be it's output.
Edit: If you take a float such as 2.00010
print(round(2.00010,5))
It will not show 0s after the last non-zero number. So output will be 2.0001 and NOT 2.00010
+ 1
Thx a lot, but I still cant fully understand that, in the 2nd example, print(round(2.00, 2)) gives 2.0, and print(round(2.0000001, 5)) gives 2.0 (I can understand that), but why print(round(2, 2)) gives 2 instead of 2.0? Utkarsh Sharma
+ 1
QWKKK Because in those examples, they were float.... While 2 is an integer.
If it was a float like 2.0 or 2.00 something it would also give the out as those example but it is an integer, 2 with no decimal point. So it will print only 2 not 2.0
0
Utkarsh Sharma I understand! Thx for your time!