Convert Float to Int in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python, there are many situations where we have to convert float values to int values. Many approaches can be used for converting float to int values:

  • conversion using int().
  • conversion using math.floor() and math.ceil().
  • conversion using round().
  • conversion using math.trunc().

How to Convert Float to Int in Python?

We can convert a float value to an int using Type conversion in Python. Type Conversion is an explicit way of changing an operand to a certain type. Python comes with a few in-built methods to achieve this.

Displaying a floating value as a number or a percentage is a frequent application for converting a float to an integer. Integer values can be shown as such without affecting the actual data type because they are immutable. So when calculating efficiency, integer values are always preferred over floating-point values.

So for all this process, we use Python inbuilt functions for converting the float value to the integer values, for example int() function, etc.

Example: Let's take a look at int values in Python

Output:

What is Float in Python?

The float type represents the floating point number in Python. A decimal point is used to separate the integer and fractional elements of the word "float," which denotes real numbers.

Python uses 64-bit double-precision numbers to represent float values. Any floating-point number can have a maximum value of approximately 1.8 x 10^308. The Python string inf will indicate any number bigger than 1.8 x 10^308. 77.44, 42.3e19, and -32.64+e12 are examples of floating point numbers.

Example: Let's take a look at float values in Python

Output:

What is int in Python?

In Python is a whole number having no decimal points. Integer values can be of any size and can be negative or positive. Int Class defines Integer values. As integer does not consist of any decimal points and hence can extend up to the computer’s limit. However, you can subsequently change it. By default, Int is decimal, or base 10, although you can later convert it. Examples of some integer values are 20, 1, -10, etc.

Example: Let's take a look at int value in Python

Output:

Methods to convert float to int in Python

For converting float to int in Python, we can use many methods. These methods use type conversion or inbuilt library functions enlisted below:

  • conversion using int()
  • conversion using math.floor() and math.ceil()
  • conversion using round()
  • conversion using math.trunc()

Method 1: Conversion using int()

Python has a variety of built-in methods that can make programming easier. One such method is int(), which quickly transforms any data type into an integer. The function trims the values after the decimal points and accepts a float-point variable as an argument. The result is an integer or whole number as the output. Let's look at the syntax of int() method:

Syntax:

Return Value:

This method returns an integer value.

Example 1: Let's take an example of how the int() method is used to convert the float value to int value:

Output:

Example 2: The int() function typically rounds off the results to the nearest integer before the decimal point. However, there are times when an exception occurs, so we cannot predict or define the behavior in those situations.

Output: It rounded out the value because there is an insignificant difference between 3.99999999999999999 and 4.0.

Method 2: Conversion using math.floor() and math.ceil()

Use the floor() method and pass the value as the method's parameter if you want to convert a float value to an int value that is no larger than the input. While using the ceil() method, you can change it to a value greater than the original.

Syntax:

Return Value: This method returns an integer value converting on the fact that ceil() is used or floor() is used.

Example:

Let's take an example of how the floor() and ceil() method is used to convert the float value to int value:

Output:

Method 3: Conversion using round()

Using the round() method, a float value can be converted to an int value, which is the closest integer value. When there is an equal difference between smaller and larger values, the larger integer is chosen.

Syntax:

Return Value: This method returns an integer value rounding the given value to the nearest integer.

Example:

Let's take an example of how the round() method is used to convert the float value to int value:

Output:

Method 4: Conversion using math.trunc()

Python has a built-in function called trunc() that helps in providing an integer value. The method takes the float-point value and ignores the remainder of the value. Let's look at the example below to see how the trunc() method is used:

Syntax:

Return Value: This method returns an integer value removing the decimal part of the value.

Example:

Let's take an example of how the trunc() method is used to convert the float value to int value:

Output:

Limitations for Converting Float to Int in Python

  • Although the int() method can be used for type conversions, it is not advised because there is a chance of data loss.
  • To prevent data loss, you can use the math module. If the function is not used properly, data loss does not change.

Conclusion

  • Many approaches can be used to convert float to int in Python.
  • We can convert a float value to an int using Type conversion.
  • The float type represents the floating point number in Python. A decimal point is used to separate the integer and fractional elements of the word "float," which denotes real numbers.
  • In Python, Integer values can be of any size. Int values can be negative or positive. Int Class can define integer values.
  • These methods use type conversion or inbuilt library functions enlisted below:
    • conversion using int()
    • conversion using math.floor() and math.ceil()
    • conversion using round()
    • conversion using math.trunc()
  • There can also be limitations while converting float to int in Python.