# Convert float to int in Python 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

Output:

16

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.

Output:

5

## 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

Output:

17
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

Output:

16
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

16
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.

## Related Posts

• 17 November

### How to check for null in Python

Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

• 17 November

### Sort dictionary by value in python

Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

• 17 November

### Get class name in Python

Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

• 17 November

### How to Print to File in Python?

Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

• 17 November

### Get Absolute Path in Python

Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

• 17 November

### Sort List of Lists in Python

Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.