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
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'>
This article explains float value conversion, and for that, this article has used the int() function.