Last Updated on August 6, 2021 by Roshan Parihar
In this tutorial, learn how to convert float to integer type value in Python. The short answer is: use int()
function to convert a positive or negative float value to an integer.
If you use only the int()
, you will get integer value without a round figure. To convert float to int with the round figure, read this tutorial to the end.
Python Convert Float to Integer Using int()
If you want to convert float to integer variable type in Python. You have to use the int()
function of Python. Pass the float variable as the argument of the int()
function in Python.
To print the resulted integer value after conversion, you have to use the Python print()
function.
1 2 |
myFloat = 10.8; print(int(myFloat)); |
Output
The above example prints the integer value in the output. However, this is the positive conversion of the positive float value. If you want to convert a negative float value to an integer, you have to read the next section of this post.
How to Change Negative Float Value to Int in Python
To change or convert the negative float value, you have to use the same int()
function. You have to also pass the float variable as the argument of the int()
in Python.
The negative float value gives the negative integer on conversion.
1 2 |
myFloat = -10.8; print(int(myFloat)); |
Output
The above example showing the negative integer value in the output after conversion. All the above examples showing the conversion from float to int without a round figure.
If you want to convert float to integer type value with round figure conversion. Read the next section of the post to learn how to how to make the conversion with a round figure.
How to Altering Float to Int in Round Figure
If you want to convert float to int type value in Python with a round figure. You have to use the round()
function of Python inside the int()
function. Pass the float variable as the argument of the round()
function in Python.
The round()
function add one to the integer value after conversion, if the digit after the decimal point is more than 5.
1 2 |
myFloat = 10.8; print(int(round(myFloat))); |
Output
The above example contains 10 as the float value before the decimal point. However, the resulted integer value after conversion gives 11 as the output. This is just because the float contains a value of more than 5 after the decimal point.
DOWNLOAD Free PYTHON CHEAT SHEET
You may also like to read
- How to create number variables of various types in Python
- Declare or create variables in python
- Limit Float To Two Decimal Places in Python
- How to Round Float To 2 Decimal Places in Python
I hope you like this tutorial on how to convert float to an integer value in Python.
References