Convert float to int in Python

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.