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
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read