How to Convert Float to Int in Python?

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. Let’s create a variable and assign a float value to it.

bookPrice = 3.50
// 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.

// 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)
// 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

// Output: <class 'int'>


This article explains float value conversion, and for that, this article has used the int() function.

Related Posts

Tags: Float, Integer, Python,