How to convert Float to Int in Python?
Last Updated :
01 Aug, 2024
Converting a float value to an int is done by Type conversion, which is an explicit method of converting an operand to a specific type. However, it is to be noted that such type of conversion may tend to be a lossy one (loss of data). Converting an int value like 2 to floating-point will result in 2.0, such types of conversion are safe as there would be no loss of data, but converting 3.4 to an int value will result in 3 leading to a lossy conversion.
Examples:
Input: 3.3
Output: 3
Input: 5.99
Output: 5
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.
Syntax: int(x)
Return: integer value
Example 1: Number of type float is converted to a result of type int.
Python
# conversion from float to int
num = 9.3
# printing data type of 'num'
print('type:',
type(num).__name__)
# conversion to int
num = int(num)
# printing data type of 'num'
print('converted value:', num,
', type:', type(num).__name__)
Outputtype: float
converted value: 9 , type: int
Example 2: The int()
function in Python truncates the decimal part of the number and keeps the integer part only. But in the given example, The value 5.99999999999999999999 is represented as 6.00000000000000000000 due to floating-point errors, resulting in the output 6 when converted to an integer. To avoid such precision issues, consider using the decimal library for extreme precision or math.floor() to handle edge cases consistently.
Python
# example of unpredictable
# behaviour of int()
num1 = 5.9
num2 = 5.99999999999999999999
num1 = int(num1)
num2 = int(num2)
print(num1, num2, sep = '\n')
Method 2: Conversion 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.
Syntax: math.floor(x)
Parameter:
x: This is a numeric expression.
Returns: largest integer not greater than x.
Syntax: math.ceil(x)
Parameter:
x: This is a numeric expression.
Returns: Smallest integer not less than x.
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
# conversion using floor and ceil .
# importing math module
import math
num = 5.6
floor_value = math.floor(num)
ceil_value = math.ceil(num)
print("the result using floor() : ",
floor_value ,
', type : ',type(floor_value).__name__)
print("the result using ceil() : ",
ceil_value,
', type: ', type(ceil_value).__name__)
Outputthe result using floor() : 5 , type : int
the result using ceil() : 6 , type: int
Method#3: Conversion 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.
Syntax: round(x)
Parameter:
x: This is a numeric expression.
Returns: integer multiple of closest.
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
# conversion using round.
num = 5.6
# Before conversion value and type
print( 'Type : ', type(num).__name__)
print( "Original number is : ", num)
# conversion to int
value = round(num)
print( 'Type : ',type(value).__name__)
print("the result using round : ",value)
OutputType : float
Original number is : 5.6
Type : int
the result using round : 6
Method#4: Conversion using math.trunc( ).
A float value can be converted to an int value. In case of negative number it behaves like ceiling function of math library and in case of positive number it behaves like floor function..
Syntax: math.trunc(x)
Parameter:
x: This is a numeric expression.
Returns: larger integer in case of negative number else in case of positive number smaller number.
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
# conversion using math.trunc().
import math
num = 5.6
num2 = -2.6
# conversion of negative number to int
value = math.trunc(num2)
print( 'Type of value : ',type(value).__name__)
print("the result using round : ",value)
# conversion of positive number to int
data = math.trunc(num)
print( 'Type of data: ',type(data).__name__)
print("the result using round : ",data)
OutputType of value : int
the result using round : -2
Type of data: int
the result using round : 5
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
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 [Tex]\pm\frac{p}{q} [/Tex] , where p<=q. There are various methods of converting between fractions and percentages. Method 1: Manual conversion of the f
3 min read
How to convert DateTime to integer in Python
Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s
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
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 Content Types
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 methods Convert float64 Columns to int64 in Pandas DataFrameTo transform a Pandas column to an i
4 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
How to convert Int data type to Float in Golang?
In Golang, the data types are bound to variables rather than the values, which means that, if you declare a variable as int, then you can store only integer type value in it, you cant assign character or string in it unless you convert the data type to required data type. To convert an integer data
2 min read