Basically, I convert float to int, but I don't always have the expected value.
Here is the code that I am executing:
x = 2.51
print("--------- 251.0") y = 251.0 print(y) print(int(y)) print("--------- 2.51 * 100") y = x * 100 print(y) print(int(y)) print("--------- 2.51 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y)) print("--------- 2.51 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y)) x = 4.02 print("--------- 402.0") y = 402.0 print(y) print(int(y)) print("--------- 4.02 * 100") y = x * 100 print(y) print(int(y)) print("--------- 4.02 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y)) print("--------- 4.02 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y))
And here is the result (the first value is the result of the operation, the second value is int () of the same operation):
2.51 and 4.02 are the only values that lead to strange behavior in the range 2.50 → 5.00. Each other value of two digits in this range is converted to int without any problems when performing the same operations.
So what am I missing that leads to these results? By the way, I am using Python 2.7.2.
python floating-point int
B. Richard Jul 04 2018-11-11T00: 00Z
source share