How to convert the float to Int in Python
In this tutorial, we will learn about how to convert the float to an Int in Python with the help of examples.
Using int() method
To convert a float value to an Int, we can use the built-in int()
method in Python.
The int()
method takes the float value as an argument and converts it to integer.
Here is an example:
afloat = 12.79
result = int(afloat)
print(result)
Output:
12
Note: When we use the int()
constructor the Integer is always rounded to the nearest downward value, like 12.752
to 12
or 6.99
to 6
.
Using trunc() method
Similarly, we can also use the built-in trunc()
method in python to convert a float to int.
The trunc()
method takes the float value as an argument and returns the integer part of it by stripping the decimal values.
Here is an example:
from math import trunc
afloat = 12.79
result = trunc(afloat)
print(result)
Output:
12
Note: We have imported the trunc() method from the python math module