
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Float to Integer in Python
In python there are two number data types: integers and floats. In general integers do not have any decimal points and base value is 10 (i.e., Decimal). Whereas floats have decimal points. Python provides some built?in methods to convert floats to integers. In this article we will discuss some of them.
Using the int() function
The int() function converts the floating point numbers to integers, by removing the decimals and remains only the integer part. Also the int() function does not round the float values like 49.8 up to 50.
Example
In the example the data after the decimal points gets eliminated and we only have the whole number (integer number).
num = 39.98 print('Data type of num:', type(num).__name__) # float to int conversion num = int(num) print('Converted value and its data type:', num,', type:', type(num).__name__)
Output
Data type of num: float Converted value and its data type: 39 , type: int
Example
The int() function takes an integer string or a float, but it does not take a float string. If a float string is given, first we need to convert it to float by using the float() function then apply it to int(). Otherwise it will raise ValueError.
num = '1.5' print('Data type of num:', type(num).__name__) # float to int conversion num = int(num) print('Converted value and its data type:', num,', type:', type(num).__name__)
Output
Data type of num: str Traceback (most recent call last): File "/home/cg/root/67537/main.py", line 6, innum = int(num) ValueError: invalid literal for int() with base 10: '1.5'
Example
In some cases the int() function behavior is not predictable, for few input float values it may round off the result to an integer less than or equal to the input float value.
num = '1.5' print('Data type of num:', type(num).__name__) # float to int conversion num = int(float(num)) print('Converted value and its data type:', num,', type:', type(num).__name__)
Output
Data type of num: str Converted value and its data type: 1 , type: int
Example
In this example the int() function rounded the float value to the next integer value.
num = 1.9999999999999999 print('Data type of num:', type(num).__name__) # float to int conversion num = int(num) print('Converted value and its data type:', num,', type:', type(num).__name__)
Output
Data type of num: float Converted value and its data type: 2 , type: int
Using the math module truncation function
Using the truncation function trunc() from the math module we can convert the floats to integers.
Example
From this example we have successfully converted the floats to integers using the math.trunc() function which works similar to the int() functions. Also we can use math.ceil(), math.floor() and ndarray..astype(int) method from numpy library to convert the floats to integer data types in python.
from math import trunc num = 7.12 print('Data type of num:', type(num).__name__) # float to int conversion num = trunc(num) print('Converted value and its data type:', num,', type:', type(num).__name__)
Output
Data type of num: float Converted value and its data type: 7 , type: int