What is wrong with the round
function in Python 3.6.3? I tested the code below:
a = 0.5
b = 0.5000001
print (round(a),round(b))
..and get result as
0 1
So rounding 0.5
-> 0
but 0.500001
-> 1
. Should both variables get value of 1
?
What is wrong with the round
function in Python 3.6.3? I tested the code below:
a = 0.5
b = 0.5000001
print (round(a),round(b))
..and get result as
0 1
So rounding 0.5
-> 0
but 0.500001
-> 1
. Should both variables get value of 1
?
It's described in docs:
If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).
Both variable will not get value 1. because the value between range from 0.0000000 to 0.5000000 will round off to 0 ,but when you increase any decimal bit after 0.5 it will round off to 1.