How To Implement Round Function In Python

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Introduction :

Python is a powerful programming language that offers excellent tools for data processing, scientific analysis, and complex modeling. One of the built-in functions in Python is round(), which returns a rounded float number. If no decimal places are specified, the function rounds the number to the nearest integer.

Python round() Function As mentioned earlier, the round() function in Python is used to round decimal values to a specified number of decimal places. If the number of decimal places is not provided, the function rounds the number to the nearest integer.

Syntax:

round(x, n)

Example:

print(round(7.6 + 8.7, 1))

Output:

16.3

Similarly, for another case:

print(round(6.543231, 2))

Output:

6.54

Practical Applications The round() function is often used to limit the number of decimal places in a given floating-point number. For example:

b = 2/6
print(b)
print(round(b, 2))

Output:

0.3333333333333333
0.33

Rounding NumPy Arrays NumPy provides additional rounding functions for handling large datasets. If you are using Python 3, NumPy is usually pre-installed. However, in Python 2, you need to install it manually:

pip install numpy

To round all values in a NumPy array, use np.around(). Example:

import numpy as np
np.random.seed(444)
data = np.random.randn(3, 4)
print(data)

Output:

[[ 0.35743992  0.3775384   1.38233789  1.17554883]
 [-0.9392757  -1.14315015 -0.54243951 -0.54870808]
 [ 0.20851975  0.21268956  1.26802054 -0.80730293]]

Other commonly used rounding functions in NumPy include:

print(np.ceil(data))  # Rounds up to the nearest integer
print(np.floor(data)) # Rounds down to the nearest integer
print(np.trunc(data)) # Truncates values to integer components
print(np.rint(data))  # Rounds to the nearest integer usin

Rounding Pandas Series and DataFrames Pandas is a widely used library for data analysis and manipulation. If you haven't installed it yet, use:

pip install pandas

Example using Pandas Series:

import pandas as pd
import numpy as np
np.random.seed(444)
series = pd.Series(np.random.randn(4))
print(series)
print(series.round(2))

Output:

0    0.357440
1    0.377538
2    1.382338
3    1.175549
dtype: float64

0    0.36
1    0.38
2    1.38
3    1.18
dtype: float64

A Pandas DataFrame is similar to a table in a database. You can round specific columns with different precisions using:

df.round({"Column 1": 1, "Column 2": 2, "Column 3": 3})

Example:

import pandas as pd
import numpy as np
np.random.seed(444)
df = pd.DataFrame(np.random.randn(3, 3), columns=["Column 1", "Column 2", "Column 3"])
print(df)
print(df.round(3))

Output:

   Column 1  Column 2  Column 3
0     0.4     0.38     1.382
1     1.2    -0.94    -1.143
2    -0.5    -0.55     0.209

Scope @ NareshIT At NareshIT’s Python Application Development Program, you will receive extensive hands-on training in front-end, middleware, and back-end technologies. The program includes phase-end and capstone projects based on real business scenarios.

Key benefits include:

  • Learning from industry experts with structured content ensuring industrial relevance.

  • Working on end-to-end applications with exciting features.

  • Gaining skills in NumPy, Pandas, and other essential Python libraries.

  • Training available for various Python certifications.

For more information, contact NareshIT for Python training through classroom or online sessions.