How to convert float to int Python

In this Python tutorial, we will see how to convert float to int Python using different examples. we will also see How to convert negative float values to int and different data types like list and array containing float values to int in Python.

What is float in Python?

In Python, a float represents a floating-point number, which is a number with a decimal point. We can create a float in Python, either directly by entering a number with a decimal point or by using operations that result in a non-integer number.

Here, is an example: Let’s take mathematic pie value in float and as well as in division.

# Defining a float
pie_in_float = 3.14
print(pie_in_float)
print(type(pie_in_float))

# Another float, resulted from a division operation
pie_division = 22 / 7
print(pie_division)
print(type(pie_division))

The output is: In the above example, pie_in_float is a float because it’s defined with a decimal point. pie_division is also a float because the division operation results in a number with decimal points.

3.14
<class 'float'>
3.142857142857143
<class 'float'>
How to convert float to int python showing float number

This is the way we can create float in Python.

What is int in Python?

In Python, int stands for integer. An integer is a whole number, positive or negative, without any decimal points. Here are a few examples of integers: 0, 1, 2, -1, -2. We can create an integer in Python by directly assigning an integer value to a variable, like this:

Suppose we have a number of states in the US in a variable.

# Defining an integer
States = 50 # number of States is US
print(States)
print(type(States))

The output is:

50
<class 'int'>
How to convert the float to integer in Python showing int

This way we can create the int in Python

How to convert float value to int Python

There are two different ways to convert float to int Python:

  • Using int() function
  • Using round() function

We will also see how to convert negative float value to int in Python.

Use the int() function to convert float to int Python

The built-in int() function is used to convert a float value to int. This function leaves the values after the decimal point and returns only the integer/whole number part.

Let’s consider the temperature in Fahrenheit during winter in some parts of Alaska:

temperature_f = -4.7
int_temperature_f = int(temperature_f)
print(int_temperature_f)
print(type(int_temperature_f))

The output is:

-4
<class 'int'>
How to convert float value to int Python using int() function

In most cases like the above example, the int() function in Python rounds off the result to an integer lesser than or equal to the input.

But in some cases, the behavior of the int() function in Python is neither definite nor predictable.

For example, we have a Python float value:

float_num = 10.9999999999999999
int_num = int(float_num)
print(int_num)
print(type(int_num))

The output is: The int() function returns the next integer value of the digit.

11
<class 'int'>
How to convert float number to integer in Python using the int() function exception

This way we can use the int() function in Python to convert the float value in int Python.

Use the round() function to convert float to int Python

The round() function is a built-in function in Python that rounds a number to the nearest value and hence we can convert float to int Python. It can operate on both integers and floating-point numbers. The round() function in Python rounds a number up if the fractional part is 0.5 or higher and down if it’s less than 0.5.

Here’s a simple usage of the round() function in Python to convert float to int Python: Suppose we went to do the shopping and the bill came in some float value and we have to pay the shopkeeper the round-off value of the bill in dollars:

Bill = 103.6 # bill is in dollars
rounded_price = round(Bill)
print(rounded_price)
print(type(rounded_price))

The output is: The float 103.6 is rounded to 104.

104
<class 'int'>
How to convert float to int in Python using round function

This way we use the round() function in Python to convert the float into int.

Note: When we convert float to int Python, the int() function truncates the decimal part of the number rather than rounding it to the nearest whole number. If we need rounding, we should use the round() function before converting to an integer.

How to convert negative float value to int in Python

Let’s see how to convert a negative float value to int in Python.

To convert the negative float value to int in Python, we have to use the int() function. Also, we have to pass the float variable as the argument of the int() in Python.

Example:

float_val = -12.8;
print(int(float_val));

You can refer to the below screenshot to see the output for how to convert negative float value to int in Python.

How to convert negative float value to int in python
How to convert negative float value to int in Python

This is how to convert negative float value to int in Python.

Let’s check how to covert different data types containing float values into int Python:

Convert different data types containing float values into Int in Python

  • Float list
  • Float array

How to convert float list to int in Python

Here, we will see how to convert float list to int in Python

To convert float list to int in Python we will use the built-in function int and it will return a list of integers.

Example:

num = [12.1, 14.2, 15.8, 17.4]
print([int(num) for num in num])

You can refer to the below screenshot to see the output for how to convert float list to int in Python.

How to convert float list to int in python
How to convert float list to int in Python

The above code, we can use to convert float list to int in Python.

Convert float array to int in Python

Now, we will see how to convert float array to int in Python.

To convert float array to int in Python we will first import numpy as np and then we will use the function astype() and it will return the integer.

Example:

import numpy as np
arr = np.array((1.4, 2.6, 3.1, 4.3))
arr = arr.astype(int)
print(arr)

You can refer to the below screenshot to see the output for convert float array to int in Python.

Convert float array to int in python
Convert float array to int in Python

The above code, we can use to convert float to int Python.

Conclusion

In Python, to convert a float to int in Python we have two basic methods: the int() function and other is the round() function. Both of these methods have their own pros and cons, we should choose them according to the requirements of the code. We have also seen how to convert negative float value to int Python and in last we have seen How to convert different data types containing float value into int in Python like Float list and float array.

You may like the following Python tutorials: