How to do float to integer conversion in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

21st Oct 2021, 4:27 AM
Yukeshan Yoganathan
Yukeshan Yoganathan - avatar
1 Answer
+ 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
21st Oct 2021, 6:38 AM
Coding Cat
Coding Cat - avatar