How to Convert Float to Integer in Python
To convert a float to an integer in Python, you can use the int() function or the round() function.
The following examples show how to convert a float to an integer in Python using these two methods.
Using int() Function
We can use the int() function to convert a float to an integer.
Suppose we have the following float value:
# Declare float value
float_value = 3.1428
# Convert float to int using int() function
number = int(float_value)
# Show number
print("Number:", number)
Output: 👇️
Number: 3
In this example, we use the int() function to convert the float float_value to an integer number. The output shows the integer value converted from the float using the int() function.
Using round() Function
We can use the round() function to convert a float to an integer.
Suppose we have the following float value:
# Declare float value
float_value = 3.1428
# Convert float to int using round() function
number = round(float_value)
# Show number
print("Number:", number)
Output: 👇️
Number: 3
In this example, we use the round() function to round off the float float_value and convert it to an integer number. The output shows the integer value converted from the float using the round() function.
Conclusion
We can use the int() function or the round() function to convert a float to an integer in Python. These methods provide a convenient way to handle numeric conversions from floats to integers.