Table of Contents
Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions.
This tutorial will discuss the several methods available to convert float to int in Python.
Using the int()
function to convert float to int in Python
The int()
function in Python takes an argument, which could be any data type that represents a number, and converts this argument into an integer. The int()
function is flexible and is capable of taking arguments ranging from string
, to float
or even the integer
itself.
In the case of floating-point numbers, the int()
function ignores the decimal part of the number and returns the integer part or the part before the decimal as the desired output.
The following code uses the int()
function to convert float to int in Python
1 2 3 4 |
x = 16.27 print(int(x)) |
Output:
The int()
function is very reliable when it comes to consistent conversions, but there is one such exception where it provides a different output. When a floating-point number has sixteen 9
digits after the decimal point, let’s say the number is x.9999999999999999
, then the int()
function returns the output as x+1
in case of a positive value and x-1
when the value is negative.
1 2 3 4 |
x = 4.9999999999999999 print(int(x)) |
Output:
Further reading:
Using the math
module functions to convert float to int in Python
The math
module can be defined as an always accessible and standard module in Python. It provides the access to the fundamental C library mathematical functions. The math
module needs to be imported to the Python program in order to use all the functions in this method.
The task of converting floating-point numbers into integers can be achieved successfully with the help of three different functions from the math
library. However, the output may be different in each case.
Using ceil()
The ceil()
function is utilized to round off a number up to an integer, wherever necessary, and provide the output.
The following code uses the ceil()
function to convert float to int in Python
1 2 3 4 5 |
import math print(math.ceil(16.27)) print(math.ceil(4.9999999999999999)) |
Output:
5
Here, in the above program code, the math.ceil()
function provides the ceiling value of the numbers given in the floating-point data type.
Using trunc()
Next, we have the trunc()
function. The trunc()
function, as its name suggests, is utilized to remove or cut the decimal part of the given number passed as the argument and returns the integer part as the desired output.
The working and behavior of the trunc()
function are exactly the same as that of the int()
method explained in the article above.
The following code implements the trunc()
function to convert float to int in Python
1 2 3 4 5 6 7 |
import math x = 16.27 y = 4.9999999999999999 print(math.trunc(x)) print(math.trunc(y)) |
Output:
5
The trunc()
function behaves the same as the int()
function even in the case of the x.9999999999999999
exception.
Using floor
Lastly, there is the floor()
function for the conversion process. It is used to return the floor value of a given number, which basically means that it rounds the given number down to the closest integer value.
The following code implements the floor()
function to convert float to int in Python
1 2 3 4 5 6 7 |
import math x = 16.27 y = 4.9999999999999999 print(math.floor(x)) print(math.floor(y)) |
5
All of the functions described above can be used for the process of conversion of floating-point numbers to integers, depending on the needs of the user.
We should note that the conversion of floating-point numbers to integers leads to data loss. For example, the number 16.5
, when converted with the int()
function returns the integer 16
. The reverse process, i.e. the conversion of an integer to a floating-point number does not lead to a loss in any data.
That’s all about how to convert float to int in Python.