In this short tutorial, we learn how to convert float to int Python. We look at all the various methods and discuss their pros and cons.
In case you are looking only for a safe solution to use in your application, I would recommend the second solution. However, if you are interested in the various methods available please follow along.
Table of Contents - Float to Int Python
- Why convert float to int in Python?
- Solution 1 - Using int()
- Solution 2 - Using the math module
- Limitations & Caveats - Float to Int Python
Why convert float to int in Python?
Converting data types is a common practice and Python comes with a few in-built methods to achieve this. However, converting Float to int in Python must be dealt with more caution; we talk more about this later in the article.
A common use case of converting a float to int is to display a floating value as a number or a percentage. Since int values are immutable the values can be displayed as integer values without changing the original data type.
Apart from all this, when it comes to the efficiency of calculations, integer values are always preferred.
Solution 1 - Using int():
This method of converting float to int in Python uses the int() method to convert float to int. However, this method is prone to data loss and hence isn't recommended.
The below example would give you a better understanding:
float_1 = 1.1
float_2 = 1.5
float_3 = 1.9
print(int(float_1))
#Output - 1
print(int(float_2))
#Output - 1
print(int(float_3))
#Output - 1
As you can see, Python converts all the values into 1 which is an integer value. The data loss here may not seem significant unless you are dealing with currency values. In that case, the inefficient rounding of the float values can cause monetary losses.
Due to this, as a common practice, this method of conversion is not recommended.
Solution 2 - Using the math module:
The math module provides access to a lot of additional mathematical functions and among them, we make use of the ‘math.ceil’ and ‘math.floor’ functions.
The ‘math.ceil’ takes a floating value as a parameter and returns the smallest integer value that is greater than or equal to the argument.
Syntax of math.ceil:
math.ceil(x)
The ‘math.floor’ function does the opposite of ‘ceil’ and returns the largest integer less than or equal to the argument.
Syntax of math.floor
math.floor(x)
Since both methods return an integer value, this second method can also be used to convert float to Int in Python.
Parameter:
x - Required, the number that you are looking to convert
Code & Explanation:
Since both the above-mentioned methods are part of the math module, it needs to be imported before we use them.
import math
float_1 = 1.1
float_2 = 1.5
float_3 = 1.9
print(math.floor(float_1))
print(math.ceil(float_1))
#Output - 1
#Output - 2
print(math.floor(float_2))
print(math.ceil(float_2))
#Output - 1
#Output - 2
print(math.floor(float_3))
print(math.ceil(float_3))
#Output - 1
#Output - 2
As you can see in the above code snippet, the values can be converted based on your preference reducing data loss.
Limitations & Caveats - Float to Int Python
- Although the int() method works, it is not recommended as there could be data loss. However, it can be used if you are just looking to understand the concept and not working on anything significant.
- The math methods prevent data loss while converting float to int in Python; however, you could still cause a loss if the correct function is not used. I would recommend practicing it a few times to ensure you have a better understanding.
Top comments (0)