How to Convert Float to Int in Python?

Last updated on February 24, 2023

Float value can be converted into an integer by using Python’s built-in function int(). It converts the first argument’s data type into an integer.

Syntax

int(value)

Patameter

  • It has one required parameter that refers to the value which needs to convert into an integer.

Usage

Let’s create a variable and assign a float value to it.

bookPrice = 3.50
print(bookPrice)
// Output: 3.5

If you want to check the data type of bookPrice variable then you can use the type() function that returns the type of a variable. To do that pass the bookPrice variable into the type() function.

print(type(bookPrice))
// Output: <class 'float'>

The type() function returns the float data type of bookPrice variable. Now it is confirmed that bookPrice variable’s data type is float. To convert it into an integer, use python’s built-in function int() and assign it to any variable. Below is an example,

convertIntoInteger = int(bookPrice)
print(convertIntoInteger)
// Output: 3

Here, the int() function converts bookPrice value into an integer as you can see its output has changed to 3. To check further its data type, pass it as an argument in the type() function. Below is the example

print(type(convertIntoInteger))
// Output: <class 'int'>

Conclusion

This article explains float value conversion, and for that, this article has used the int() function. The int() function takes a string or number value in an argument and converts it into an integer.


Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Float, Integer, Python,