0
How to do float to integer conversion in python?
Code: X=3.999999 Y=int(X) Print(Y) Output: 3 Code: X=3.9999999999999999 Y=int(X) Print(Y) Output: 4 What is the theory behind this two codes? And how to understand the logic of this?
1 Resposta
+ 3
This will be happen because of the bit-wise representation of dezimal places. To avoid this issue, you should use the math.floor() function (or math.ceil()). Like this :
import math
varFloat = 47.11
print(varFloat) # -> 47.11
varInt = math.ceil(varFloat)
print(varInt) # -> 48
varInt = math.floor(varFloat)
print(varInt) # -> 47