What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python Program to Convert Float to Int

  • Feb 22, 2022
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
Python Program to Convert Float to Int

Irrespective of what language you are programming in, data types play a major part in your program. There are various types of programming language to deal with and hence, there are times when you get stuck to converting them into one form to another. In this article, let us study how you can convert float data type to int data type in python using various external methods and their respective outputs. But before jumping to type conversions, let us have a flashback to the introduction of float and integer data types.

What is Integer in Python?

Integer in python can be treated as the whole number which can be either positive or negative. As an integer is considered to be the whole number, it does not consist of any decimal points and hence can extend up to the computer’s limit. Int is by default decimal, i.e., base 10 although you can later convert it from one form to another. Let us understand int data type better with the example below:

For example:

a = 10
print(type(a))
print(a)

 

Output:

<class 'int'>
10

 

What is Float in Python?

Well, the float-point data type is no more different than an integer in python. Unlike integer data types, float-point consist of any number with a decimal point. According to IEEE 754 standards, the float-point data type is a 64-bit double-precision number. You can generally define them with the binary fractions(base-2) and later convert them to any other form using type conversion.

For example:

a = 3.14
print(type(a))
print(a)

 

Output:

<class 'float'>
3.14

 

How to Convert Float to Int in Python?

Below are some of the methods by which you can convert float to int in python.

1) Using int() method

Python provides multiple in-built methods which help you to ease your programming. Int() is one such method used to convert any data type into an integer with minimal steps. The function takes the float-point variable as the argument and trims the values after the decimal points. As the result, the final output will be the integer or whole number. Let's understand the working of the int() method with the example below:

For example:

number = 6.2
number = int(number)
print(number)

 

Output:

6

 

Remember that in most cases, the int() function rounds off the results to the integer lesser than the decimal point. However, sometimes there’s an exception hence we cannot predict or define the behavior for the same.

For example:

number = 6.99999999999999999
number = int(number)
print(number)

 

Output:

7

 

2) Using math.floor() and math.ceil()

If you have already decided to convert float value to an int value no larger than the input, make use of the floor() method and pass the value as the parameter to the method. Whereas, if you wish to convert it to a value greater than the original, make use of the ceil() method as shown in the example below:

For example:

import math	

num = 6.2

floor_val = math.floor(num)

ceil_val = math.ceil(num)

print("Result using floor method : ",floor_val)

print("Result using ceil method : ",ceil_val)

 

Output:

Result using floor method :  6
Result using ceil method :  7

 

Note that floor() and ceil() methods are the part of math() library and therefore, you need to import the math library before using it.

 

3) Using trunc() method

Python provides the in-built function i.e., trunc() method which helps to provide the integer value. Here, the method takes the float-point value as the parameter and ignores everything after the decimal point. Let us understand the use of trunc() method in the example below:

For example:

import math	

num = 6.2

results = math.trunc(num)
print(results)

 

Output:

6

 

4) Using numpy library

Till now, you studied how to convert a single floating-point to an integer value. But what if you are provided with the collection of multiple float values and need to get the corresponding integer values from it. In this case, you have to import the NumPy library and make use of the astype() method. Doing this will provide you with the integer value as shown in the below example:

For example:

import numpy as np
Array = np.Array((2.4, 3.6, 4.1, 6.3))
Array = Array.astype(int)
print(Array)

 

Output:

[2, 3, 4, 6]

 

Limitations for Converting Float to Int in Python

  • Although you use the int() method for type conversions it is not recommended to use as there could be the chances of data loss.
  • You can make use of the math module to avoid the loss of data. However, the situation of data loss does not change if the function is not used wisely.

Conclusion

Type conversion plays an important role while programming your code irrespective of the language you use. In this article, we studied how you can convert float to int in python using various explicit methods and their corresponding example. It is recommended to get good command on these conversions for better and more efficient programming practice. To learn more about such type conversions in python, visit our blogs at favtutor

 

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.