Examples
- Palindrome Program in Python (Check String is Palindrome or Not)
- Program to Remove Punctuations From String in Python
- Remove a Character from String in Python (Program With Examples)
- Remove Stop Words from String in Python Using NLTK and spaCy
- Program to Sort Words in Alphabetical Order in Python (Arrange in Alphabetic Order)
- How to Sort Strings in Python? (Program With Examples)
- How to Count Vowels in Python String? Vowels Program in Python
- How to Remove Vowels from String in Python? Program With Examples
- How to Convert String to Int or Float in Python? String Parse Program
- How to Convert Float to Int in Python? Program With Examples
- How to Convert Int to String in Python? Program with Examples
- Remove Spaces from String in Python (Trim Whitespace)
- Python Program to Check If Two Strings are Anagram
- How to Capitalize First Letter in Python? String Capitalization Program
- Find All Permutations of String in Python (Programs and Examples)
- Find All Substrings of a String in Python (Programs with Examples)
- Create Multiline String in Python (With & Without New Line)
How to Convert Float to Int in Python? Program With Examples
Let’s learn how to convert float to int in Python. Whether you're a beginner seeking to expand your Python skills or an experienced programmer looking for efficient ways to handle numeric data, this tutorial is for you.
The task to convert float to int in Python is common in various data processing scenarios. Understanding the conversion process and its practical applications enhances your ability to manipulate and transform numeric data effectively.
Throughout this tutorial, we will provide clear explanations, code examples, and use cases to understand the utility of float-to-int conversion in Python.
Following are the scenarios where this conversion is useful:
-
Data Analysis and Visualization: When working with large datasets containing decimal numbers, converting float values to integers can help simplify calculations and improve performance. It is particularly useful when dealing with financial data, statistical analysis, or visualizing data using charts and graphs.
-
User Input and Output Formatting: In scenarios where user input involves decimal values that need to be transformed into whole numbers, converting float to integer in Python ensures data consistency and meets specific application requirements.
-
Control Structures and Iterations: Converting floating-point values to integers is essential when working with control structures, such as loops and conditional statements. It enables precise counting, indexing, and control flow operations by dealing exclusively with whole numbers.
To practice and implement the concepts covered in this tutorial, you can use the online Python compiler provided by Tutorials Freak. It offers a user-friendly and interactive coding environment that allows you to write, run, and experiment with Python Programs directly in your web browser.
Python Program to Convert Float to Int (Number)
To convert a floating-point number to an integer in Python, you can use the `int()` function.
Code
# Take a float input from the user
float_num = float(input("Enter a floating-point number: "))
# Convert the float to an integer
int_num = int(float_num)
# Print the converted integer
print("The converted integer is:", int_num)
Output
Enter a floating-point number: 7.3636353
The converted integer is: 7
Explanation
-
In this program, the `input()` function is used to get a float input from the user. Then, the `float()` function is used to convert the input to a float data type.
-
After that, the `int()` function is used to convert the float to an integer. Finally, the converted integer is printed using the `print()` function.
Keep in mind that when converting a float to an integer, the decimal part of the float is truncated (removed), not rounded. For example, if the input is `3.7`, the resulting integer will be `3`, not `4`.
Convert Float to Int in Python Using NumPy
To convert a floating-point number to an integer in Python using NumPy, you can use the `numpy.floor()` or `numpy.ceil()` function, depending on whether you want to round down or round up the float.
Code
import numpy as np
# Take a float input from the user
float_num = float(input("Enter a floating-point number: "))
# Convert the float to an integer using floor function
int_num_floor = np.floor(float_num)
# Convert the float to an integer using ceil function
int_num_ceil = np.ceil(float_num)
# Print the converted integers
print("Converted integer using floor:", int_num_floor)
print("Converted integer using ceil:", int_num_
Output
Enter a floating-point number: 9.99999999
Converted integer using floor: 9.0
Converted integer using ceil: 10.0
Explanation
-
In this program, we first import the NumPy library using the `import numpy as np` statement.
-
Then, we use the `input()` function to get a float input from the user and store it in the variable `float_num`.
-
Next, we convert the float to an integer using the `numpy.floor()` function, which rounds down the float to the nearest integer. The result is stored in the variable `int_num_floor`.
-
Similarly, we also convert the float to an integer using the `numpy.ceil()` function, which rounds up the float to the nearest integer. The result is stored in the variable `int_num_ceil`.
-
Finally, we print the converted integers using the `print()` function.
Float to Int in Python Pandas
To convert a floating-point number to an integer in Python using the pandas library, you can use the `astype()` method provided by pandas DataFrame or Series.
Code
import pandas as pd
# Create a pandas Series with float values
float_series = pd.Series([3.7, 2.1, 5.8, 1.2, 4.9])
# Convert the float values to integers
int_series = float_series.astype(int)
# Print the converted integers
print("Original float series:")
print(float_series)
print("\nConverted integer series:")
print(int_series)
Output
Original float series:
0 3.7
1 2.1
2 5.8
3 1.2
4 4.9
dtype: float64
Converted integer series:
0 3
1 2
2 5
3 1
4 4
dtype: int64
Explanation
-
In this program, we first import the pandas library using the `import pandas as pd` statement. Then, we create a pandas Series called `float_series` with float values.
-
Next, we use the `astype()` method of the `float_series` to convert the float values to integers. The `astype()` method converts the data type of the Series elements to the specified type, in this case, `int`.
-
Finally, we print the original float series and the converted integer series using the `print()` function.
The `astype()` method can also be used with pandas DataFrame to convert float columns to integer columns. The syntax is similar, where you select the specific column or columns and apply the `astype()` method to them.