How to convert Float to Int in Python?
A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such a conversion may tend to suffer from loss (loss of data). When converting an integer value, like 2, to a floating-point value, the outcome is 2.0. This conversion method is secure since no data is lost, but when converting 3.4 to an integer, the outcome is 3, which leads to a lossy conversion.
Input: 6.77 Output: 6 Input: 4.3 Output: 4
Method 1: Using int() to convert
We can change a float value to an int using the built-in int() method, which eliminates any values past the decimal point and returns the integer/whole number component.
Syntax: int(x) Return: numerical value
Example 1: A float number is converted to an outcome of type int
# switching from a float to an int number=8.6 # printing the data type as "number" print('type:', type(number).__name__) # conversion to int number = int(number) # printing the data type as "number" print('converted value:', number, ', type:', type(number).__name__)
type: float converted value: 8, type: int
The int() function often rounds off the result to an integer that is less than or equal to the input, even though its behaviour is neither consistent nor predictable.
number1 = 4.9 number2 = 6.99999999999999999999 number1 = int(number1) number2 = int(number2) print(number1, number2, sep = '\n')
Method 2: Using math.floor() and math.ceil() to convert
Using math, a float value can be transformed into the smallest integer smaller than the input.floor() function and into the largest integer bigger than the input using math.ceil() function. The math module needs to be imported to use these techniques.
Syntax: math.floor(x) Parameter: x: This expression uses numbers. Returns: greatest number not exceeding x Syntax: math.ceil(x) Parameter: x: It's a mathematical expression. Returns: the smallest integer that is not zero.
In the example below, the floor() and ceil() methods were used to convert a float to an integer. While the latter provides the lowest integer larger than the input, the former returns an integer no larger than the input.
import math number = 6.5 floor value = math.floor(number) ceil_value = math.ceil(number) print("the result using floor() : ", floor_value , ', type : ',type(floor_value).__name__) print("the result using ceil() : ", ceil_value, ', type: ', type(ceil_value).__name__)
the result using floor(): 6, type: int the result using ceil(): 5, type: int
Method 3: Round conversion
A float value can be converted to an int value, the closest integer value if the second parameter is omitted. The greater integer is used when the difference is equal.
Syntax: round(x) Parameter: x: This expression uses numbers. Returns: nearest integer multiple.
The round() function was used in the example below to convert from float to int; the latter produces an int number that is most like.
number =7.5 # Ahead of time value and type print( 'Type : ', type(number).__name__) print( "Original number is : ", number) # conversion to int value = round(num) print( 'Type : ',type(value).__name__) print("the result using round : ",value)
Type: float Original number is: 7.5 Type: int the result using round: 5
Method 4: Math.trunc() conversion
A float value can be converted into an int value. Negative integers behave like the ceiling function in the math library, and positive numbers are like the floor function.
Syntax: math.trunc(x) Parameter: x: This expression uses numbers. Returns: If the number is negative, the integer should be larger; if it is positive, the integer should be smaller.
In the example below, the math.trunc() function has been used to convert from float to int. When a positive number is given, the latter produces a smaller integer number, while the former returns a larger integer number when a negative number is given.
import math number = 6.4 number2 = -1.4 # negative numbers converted to integers value = math.trunc(number2) print( 'Type of value : ',type(value).__name__) print("the result using round : ",value) # positive number converted to integers data = math.trunc(number) print( 'Type of data: ',type(data).__name__) print("the result using round : ",data)
Type of value: int the result using round: -1 Type of data: int the result using round: 6