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.

import_contacts

Related Posts

• 27 December

Create Array of All NaN Values in Python

Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63daec4f24e65255100533/] [crayon-63daec4f24e6c796691549/] We used numpy.empty() […]

• 27 December

Call Function from Another Function in Python

Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63daec4f250ba482115665/] [crayon-63daec4f250c0904131960/] To call a function, […]

• 27 December

[Fixed] NameError Name ‘unicode’ is Not Defined in Python

Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63daec4f2520c582415302/] [crayon-63daec4f2520f964667305/] Unicode is a computing industry standard that ensures that text from most of […]

• 26 December

Repeat List N Times in Python

Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

• 26 December

Get First Day of Next Month in Python

Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

• 25 December

Convert String Array to Int Array in Python

Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.