How to Convert Float to Int in Python?

In Python or any other programming language, data types are used to input different types of values in a program. The most commonly used data types are string, integer, float, character, etc.

Data types can be converted from one type into another using Python’s inbuilt functions such as str(), int(), float(), etc.

In this Python tutorial, different methods are explained with examples to convert float type value into an int. The following methods are elaborated on in this tutorial:

So, let’s begin this tutorial!

How to Convert Float to Int in Python?

Python provides multiple inbuilt functions to convert float values into integers. Some of the inbuilt functions are discussed in this post via practical examples.

Let’s have a look at the given below method to convert float to int in Python:

Method 1: Using int() Function

In the code given below, the inbuilt “int()” function is utilized to convert the float to int. The input and output data types are also verified using the “type()” function.

Code:

number = 5.673
print('Float Value: ',number)
print(type(number))

#using int() function
number = int(number)
print('\nInteger Value: ',number)
print(type(number))

In the above output:

  • The float number is initialized.
  • The “int()” function takes the float value as an argument and returns an integer type value.
  • The “type()” function is used to check the data type of the given number before and after conversion.

Output:

From the above output, it is verified that the float is converted into an int.

Note: The “int()” function rounds off the given number lesser than or equal to the given number. But in some cases, when there is a higher order of decimal digits(e.g., 5.9999999999999999), then it rounds up the number (6).

Method 2: Using math.floor() Function

In the example code given below, the “math.floor()” function of the math module converts the float value into an integer value.

Code:

import math

number = 2.73
print('Float Value: ',number)
print(type(number))

#using floor() function
output = math.floor(number)
print("\nInteger Value: ",output)
print(type(output))

In the above code:

  • To access the “math.floor()” function in Python script the module named “math” is imported in the program at the beginning.
  • The float value is initialized and stored in a variable named “number”.
  • The “math.floor()” function converts float value into integer. It returns the nearest largest integer number not greater than the input number.

Output:

From the above output, it is verified that the float is converted into an int using math.floor() function.

Method 3: Using math.ceil() Function

In the example code given below, the “math.ceil()” function is used to convert the float value into an integer value by rounding up to the nearest integer.

Code:

import math

number = 9.73
print('Float Value: ',number)
print(type(number))

#using floor() function
output = math.ceil(number)
print("\nInteger Value: ",output)
print(type(output))

In the above code:

  • The float value is initialized and stored in a variable named “number”.
  • The “math.ceil()” function converts the float into an integer. Whenever the input number is greater than or equal to the nearest integer number, it returns the closest integer number.

Output:

It is verified from the above output that the float is converted into an int using math.ceil() function.

Method 4: Using the round() Function

In the example code given below, the “round()” function is used to convert float to int in Python.

Code:

number = 4.6232
print('Float Value: ',number)
print(type(number))

#using round() function
output = round(number)
print("\nInteger Value: ",output)
print(type(output))

In the above code:

  • The float value is initialized and converted into float value using the “round()” function.
  • The “round()” number rounds the number to the nearest integer when the no value is passed in the second argument.
  • By using this function, the float value is converted into an int.

Note: The round() function takes only one parameter and rounds the specified number based on the fractional part. For instance, if the decimal part is greater than or equal to “.5”, then the round() function will round the given number upward, else it will round the number downward.

Output:

In the above output, the float value is converted into the integer value using the round() function.

Method 5: Using math.trunc() Function

In the example code shown below, the “math.trunc()” takes the float value and returns the truncated integer value.

Code:

import math
pos_number = 4.432
neg_number = -9.822

output = math.trunc(pos_number)
print("\nInteger Value of positive float number: ",output)
print(type(output))

output = math.trunc(neg_number)
print("\nInteger Value of negative float number: ",output)
print(type(output))

In the following code:

  • The math module is imported at the start of the program code.
  • The positive and negative “float” value is initialized in the program.
  • The “math.trunc()” takes the positive float variable and retrieves the truncated integer part of an input number.
  • Similarly, the same function is used for a negative float number.

Note: Remember that the “trunc()” function only truncates the decimal part of the number. It will not round up/down to the closest integer.

Output:

The above output shows that the float number has been successfully converted into an integer using math.trunc() function.

That’s all from this Python Tutorial!

Conclusion

To convert float to int in Python, the inbuilt functions such as “int()”, “math.floor()”, “math.ceil()”, “round()”, and “math.trunc()” are used. The inbuilt “int()” function takes a floating point value and returns it into an integer type. The “math.ceil()” and “math.floor()” functions return the nearest integer value by rounding up or down, respectively. The “round()” function rounds the number according to the standard rounding rule. Last, the “math.trunc()” function truncates the decimal value of and retrieves the integer.

This Python tutorial demonstrated a detailed overview of the float-to-integer conversion.