How to Convert Float to Integer in Python

To convert a float to an integer in Python, you can use the int() function. Simply pass the float value as an argument to int() and it will return the integer value.

The int() function in Python is used to convert a number or a string to an integer. When you pass a float value to the int() function, it will automatically truncate the decimal part and return the integer part of the number.

Example of Using int()

float_num = 63.8
int_num = int(float_num)
print(type(int_num), int_num) # <class 'int'> 63

In this example, the float value 63.8 is converted to an integer using the int() function. The output of this code will be 63. We also use the type() function to verify if it has been converted to an integer.

Note that when converting a float to an integer using int(), the decimal part of the float is truncated (not rounded). So, 3.14 becomes 3, and -3.14 becomes -3.

Handling rounding:

If you want to round the float value to the nearest integer instead of truncating the decimal part, you can use the round() function along with the int() function. Here’s an example:

float_num = 3.9
int_num = int(round(float_num))
print(int_num) #4

In this example, the float value 3.9 is rounded to the nearest integer using the round() function before converting it to an integer. Using round(), 3.5 will convert to 4 and 3.14 to 3.

You can also convert a float to an integer using the math.floor() function from the math module, which rounds down to the nearest integer:

import math

# Round down float to integer
float_num = 3.7
floor_num = math.floor(float_num)
print(floor_num) # Output: 3

Here, math.floor(3.14) rounds down the float to the nearest integer, which is 3.

Handling NaN and infinity

If the floating-point number is not a valid number (NaN) or infinity, converting it to an integer will raise a ValueError or a OverflowError. You should handle such cases appropriately in your code.

import math

nan_float = float('nan')
try:
int_num = int(nan_float)
except ValueError as e:
print(e) # Output: cannot convert float NaN to integer

infinity_float = float('inf')
try:
int_num = int(infinity_float)
except OverflowError as e:
print(e) # Output: cannot convert float infinity to integer

Handling negative numbers

When converting negative floating-point numbers to integers, the decimal part is truncated towards negative infinity. For example, int(-3.14) will result in -4, not -3. Keep this behavior in mind when working with negative numbers. You can also use the round() function to do the opposite.

Handling large floats

If you are dealing with very large floating-point numbers, converting them to integers using the int() function may result in an overflow error. In such cases, you can use the math.trunc() function, which truncates the float to an integer without raising an overflow error.

Alternative Methods

  1. math.ceil():
import math

float_num = 3.14
ceil_num = math.ceil(float_num)
print(ceil_num)  # Output: 4

The math.ceil() function rounds up the float to the nearest integer. In this example, 3.14 is rounded up to 4.

  1. math.trunc():
import math

float_num = 3.14
trunc_num = math.trunc(float_num)
print(trunc_num) # Output: 3

The math.trunc() function truncates the decimal part of the float and returns the integer part. It works similarly to int() but is available in the math module.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 125

Leave a Reply

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