All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Convert Float To Int Python

Last Updated : Mar 11, 2024

Convert Float To Int Python

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)
  1. In this method, we can use round() method to round the float value in to nearest integer
  2. In this code, we can declare a float value.
  3. 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.
  4. 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)
  1. In this method, we can use floor division method to convert float in to int in python
  2. Floor division get the integer part of the float values
  3. 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)
  1. In this method, we can use ceil division to converting float number to int number in python.
  2. Ceil function also gives the integer value
  3. In this code, we can use 4.2 as a input. This input give closer value i.e 5
  4. In ceil function, we return a closer value of input means we can add 1 in integer part.
  5. 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)
  1. In this method, we can use typecasting to converting float numbers to int numbers in python using int method
  2. It converting float value in to integer using int method
  3. In this method, we can only take the value of before decimal point.
  4. 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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪