In this article we will show you the solution of convert float to int python, in Python, there are several methods to convert a float to an integer, including using the round(), floor division, ceil function, and typecasting.
These methods make it easy to get an integer value from a float value.
Step By Step Guide On Convert Float To Int Python :-
Method 1 - Using round() method to convert float to int in python
a=2.34 b= round(a) print(b)
- In this method, we can use round() method to round the float value in to nearest integer
- In this code, we can declare a float value.
- For ex. If the number is 3.8, so the output of this number is “4” because the number after the decimal is greater than 5.
- In this code the float number is 2.34, all the numbers after decimal is less than 5 that’s why the output is “2” without any change
Method 2 - Using floor division to convert float to int in python
a=2.4 b=a//1 print(b)
- In this method, we can use floor division method to convert float in to int in python
- Floor division get the integer part of the float values
- In this code, If we can use floor division by 1 then the output is 2.
Method 3 - Using ceil division to convert float to int in python
import math a=4.2 b=math.ceil(a) print(b)
- In this method, we can use ceil division to converting float number to int number in python.
- Ceil function also gives the integer value
- In this code, we can use 4.2 as a input. This input give closer value i.e 5
- In ceil function, we return a closer value of input means we can add 1 in integer part.
- The output of this code is 5.
Method 4 - Using typecasting to convert float to int in python
a=2.76 b=int(a) print(b)
- In this method, we can use typecasting to converting float numbers to int numbers in python using int method
- It converting float value in to integer using int method
- In this method, we can only take the value of before decimal point.
- For ex- If we pass a input value as 2.7 so the output gives only 2.
Conclusion :-
In conclusion, converting a float to an integer in Python is a simple task that can be using various methods, including round(), floor division, ceil function, and typecasting.
These methods make it easy to get an integer value from a float value.
By using round() method to round the float value in to nearest integer, floor division method to convert float in to int in python, ceil function return a closer value of input means we can add 1 in integer part, typecasting method convert float to int in python using int method.
I hope this article on convert float to int python helps you and the steps and method mentioned above are easy to follow and implement.