How to convert Float to Int in Python?
Last Updated :
10 May, 2025
In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.
For example:
- Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.
- But converting 3.4 results in 3 in which the fractional part is lost.
Let's explore different methods of doing it in Python:
Method 1: Conversion using int():
To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part.
Example 1: Simple conversion of float to int:
Python
n = 9.3
print('Original type:', type(n).__name__)
n = int(n)
print('Converted value:', n, ', Type:', type(n).__name__)
OutputOriginal type: float
Converted value: 9 , Type: int
Explanation:
- 9.3 is a float.
- int(num) removes .3 and returns 9.
- Type changes from float to int.
Example 2: Precision Conversion
The int() function in Python removes the decimal part and keeps only the whole number. However, due to how floating-point numbers are stored in memory, a value like 5.99999999999999999999 might be treated as 6.0. So, converting it to an integer gives 6.
To avoid such precision issues, we can use the decimal module for better accuracy or math.floor() to always round down.
Python
n1 = 5.9
n2 = 5.99999999999999999999
print(int(n1))
print(int(n2))
Explanation:
- num2 appears to be less than 6, but due to floating-point precision, it's stored as 6.0.
- int(num2) becomes 6.
Method 2: Using math.floor() and math.ceil()
A float value can be converted to an int value no larger than the input by using the math.floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math.ceil() function. The math module is to be imported in order to use these methods.
Example : In the below example conversion from float to int has been achieved using the floor() and ceil() methods, the former returns an int no larger than the input and the latter returns the smallest integer larger than the input.
Python
import math
n = 5.6
f = math.floor(n)
c = math.ceil(n)
print("Using floor():", f, ', Type:', type(f).__name__)
print("Using ceil() :", f, ', Type:', type(c).__name__)
OutputUsing floor(): 5 , Type: int
Using ceil() : 5 , Type: int
Explanation:
- math.floor(5.6) returns 5 (rounds down).
- math.ceil(5.6) returns 6 (rounds up).
- Both return int types.
Method 3: Using round()
A float value can be converted to an int value which is closet integer value if does not pass second parameter. In case of equal difference it goes toward larger integer.
Example: In the below example conversion from float to int has been achieved using the round() methods, the former returns an int number which is closest to number.
Python
n = 5.6
print('Original type:', type(n).__name__)
print('Original number:', n)
val = round(n)
print('Rounded type:', type(val).__name__)
print('Rounded value:', val)
OutputOriginal type: float
Original number: 5.6
Rounded type: int
Rounded value: 6
Explanation:
- round(5.6) returns 6 ,closest integer.
- If number ends in .5, Python rounds to the nearest even number.
Method 4: Using math.trunc()
The math.trunc() function truncates the float — similar to int(), but comes from the math module.
Example: In the below example conversion from float to int has been achieved using the math.trunc() methods, the former returns an larger int number which in case of negative number, else in case of positive number return smaller integer number.
Python
import math
n1 = 5.6
n2 = -2.6
# Positive number
data = math.trunc(n1)
print("Truncated (positive):", data, ', Type:', type(data).__name__)
# Negative number
val = math.trunc(n2)
print("Truncated (negative):", val, ', Type:', type(val).__name__)
OutputTruncated (positive): 5 , Type: int
Truncated (negative): -2 , Type: int
Explanation:
- math.trunc(5.6) returns 5 (cuts off decimal).
- math.trunc(-2.6) returns -2 (still just removes the fraction).
- Behaves like int(), but from math.
Similar Reads
How to Convert Bytes to Int in Python?
Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
How to convert fraction to percent in Python?
Fractions are a part of a whole commodity that can be expressed in the form of a numerator divided by the denominator. A fraction is of the form \pm\frac{p}{q} , where p<=q. There are various methods of converting between fractions and percentages. Method 1: Manual conversion of the fraction to p
3 min read
How to Convert Int to Bytes in Python?
The task of converting an integer to bytes in Python involves representing a numerical value in its binary form for storage, transmission, or processing. For example, the integer 5 can be converted into bytes, resulting in a binary representation like b'\x00\x05' or b'\x05', depending on the chosen
2 min read
Convert hex string to float in Python
Converting a hex string to a float in Python involves a few steps since Python does not have a direct method to convert a hexadecimal string representing a float directly to a float. Typically, a hexadecimal string is first converted to its binary representation, and then this binary representation
3 min read
How to convert String to Float in PHP ?
Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string
3 min read
Convert String to Float in Python
The goal of converting a string to a float in Python is to ensure that numeric text, such as "33.28", can be used in mathematical operations. For example, if a variable contains the value "33.28", we may want to convert it to a float to perform operations like addition or division. Let's explore dif
2 min read
How To Convert Data Types in Python 3?
Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of
4 min read
How to convert string to integer in Python?
In Python, a string can be converted into an integer using the following methods : Method 1: Using built-in int() function: If your string contains a decimal integer and you wish to convert it into an int, in that case, pass your string to int() function and it will convert your string into an equiv
3 min read
How to Convert float64 Columns to int64 in Pandas?
float64 represents a floating-point number with double precision and int64 represents a 64-bit integer number. In this article, we will learn to Convert float64 Columns to int64 in Pandas using different methodsConvert float64 Columns to int64 in Pandas DataFrameTo transform a Pandas column to an in
3 min read
How To Convert Unicode To Integers In Python
Unicode is a standardized character encoding that assigns a unique number to each character in most of the world's writing systems. In Python, working with Unicode is common, and you may encounter situations where you need to convert Unicode characters to integers. This article will explore five dif
2 min read