In this python tutorial, you will learn about how to convert float to int in python and, also we will check:
- How to convert float to int python
- How to transform float to int python
- How to convert float list to int in python
- How to convert float to whole number python
- Python convert float to int ceil
- Convert float array to int in python
- Convert float to int python numpy
- How to convert negative float value to int in python
- How to convert float variable to int in python
How to convert float to int python
Let’s see how to how to convert float to int python
To convert a float to int in python, we will use the built-in int() function. This function will return the integer number part.
Example:
num = 5.4
num = int(num)
print(num)
You can refer to the below screenshot to see the output for how to convert float to int python.
This is how to convert float to int in Python.
Read: How to convert string to float in Python
How to transform float to int Python
Now, we will see how to transform float to int python
In this example, we will use trunc() function. The trunc() function returns the integer part of the number. It ignores everything after the decimal points.
Example:
from math import trunc
print(trunc(14.2))
You can refer to the below screenshot to see the output for how to transform float to int python.
The above Python code we can use to transform float to int in Python.
Read Python convert binary to decimal
How to convert float list to int in Python
Here, we will see how to convert float list to int in python
To convert float list to int in python we will use the built-in function int and it will return a list of integers.
Example:
num = [12.1, 14.2, 15.8, 17.4]
print([int(num) for num in num])
You can refer to the below screenshot to see the output for how to convert float list to int in python.
The above code, we can use to convert float list to int in Python.
Read Python Count Words in File
How to convert float to whole number python
Let’s see how to convert float to whole number python
In this example, we will use the built-in round() function which will round up the value and returns the integer value.
Example:
num = round(8.4598)
print(num)
You can refer to the below screenshot to see the output for how to convert float to whole number python.
The above code, we can use to convert float to whole number in Python.
Python convert float to int ceil
Let us see how to convert float to int ceil.
In this example, we will use ceil() function which will rounds up the next full integer value.
Example:
from math import ceil
print(ceil(15.7))
You can refer to the below screenshot to see the output for python convert float to int ceil.
This is how to convert float to int ceil in Python.
Convert float array to int in python
Now, we will see how to convert float array to int in python.
To convert float array to int in python we will first import numpy as np and then we will use the function astype() and it will return the integer.
Example:
import numpy as np
arr = np.array((1.4, 2.6, 3.1, 4.3))
arr = arr.astype(int)
print(arr)
You can refer to the below screenshot to see the output for convert float array to int in python.
The above code, we can use to convert float array to int in python.
Convert float to int Python numpy
Here, we will see how to convert float to int python numpy.
In this example, we will import numpy as np and the built-in function astype() is used to convert float to int python numpy.
Example:
import numpy as np
arr = np.array([[5.3, 6.5, 7.234], [30.3, 33.543, 35.995]])
arr = arr.astype(int)
print(arr)
You can refer to the below screenshot to see the output for convert float to int python numpy.
The above Python code, we can use to convert float to int Python numpy.
How to convert negative float value to int in python
Let’s see how to convert negative float value to int in python.
To convert the negative float value to int in python, we have to use the int() function. Also, we have to pass the float variable as the argument of the int() in python.
Example:
float_val = -12.8;
print(int(float_val));
You can refer to the below screenshot to see the output for how to convert negative float value to int in python.
This is how to convert negative float value to int in python.
How to convert float variable to int in python
Now, we will see how to convert float variable to int in python.
To convert float variable to int in python, we will use the int() function. Also, we have to pass the float variable as the argument of the int() in python, and to get the output use the print() function.
Example:
float_var = 18.8;
print(int(float_var));
You can refer to the below screenshot to see the output for how to convert float variable to int in python.
The above code, we can use to convert float variable to int in python.
You may like the following Python tutorials:
- Python convert dictionary to list
- How to find area of a triangle in Python
- Python program to find an area of a rectangle
- How to calculate area of a circle in Python
- Add string to list Python
In this Python tutorial, we have learned about how to convert float to int python. Also, we covered these below topics:
- How to convert float to int python
- How to transform float to int python
- How to convert float list to int in python
- How to convert float to whole number python
- Python convert float to int ceil
- Convert float array to int in python
- Convert float to int python numpy
- How to convert negative float value to int in python
- How to convert float variable to int in python
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.