For working professionals
For fresh graduates
More
13. Print In Python
15. Python for Loop
19. Break in Python
23. Float in Python
25. List in Python
27. Tuples in Python
29. Set in Python
53. Python Modules
57. Python Packages
59. Class in Python
61. Object in Python
73. JSON Python
79. Python Threading
84. Map in Python
85. Filter in Python
86. Eval in Python
96. Sort in Python
101. Datetime Python
103. 2D Array in Python
104. Abs in Python
105. Advantages of Python
107. Append in Python
110. Assert in Python
113. Bool in Python
115. chr in Python
118. Count in python
119. Counter in Python
121. Datetime in Python
122. Extend in Python
123. F-string in Python
125. Format in Python
131. Index in Python
132. Interface in Python
134. Isalpha in Python
136. Iterator in Python
137. Join in Python
140. Literals in Python
141. Matplotlib
144. Modulus in Python
147. OpenCV Python
149. ord in Python
150. Palindrome in Python
151. Pass in Python
156. Python Arrays
158. Python Frameworks
160. Python IDE
164. Python PIP
165. Python Seaborn
166. Python Slicing
168. Queue in Python
169. Replace in Python
173. Stack in Python
174. scikit-learn
175. Selenium with Python
176. Self in Python
177. Sleep in Python
179. Split in Python
184. Strip in Python
185. Subprocess in Python
186. Substring in Python
195. What is Pygame
197. XOR in Python
198. Yield in Python
199. Zip in Python
The round function in Python is a simple yet powerful tool that helps you round floating-point numbers to the nearest integer or to a specified number of decimal places. Whether you are working on financial applications, scientific computations, or basic arithmetic, this Python function provides accurate and consistent results.
In this article, we will explore the syntax, usage, and variations of the round() function in Python. You will also learn how to use the math and NumPy libraries for advanced rounding, and how to handle lists, negative numbers, and DataFrames. Each concept is paired with real examples, outputs, and detailed explanations to make things easy and practical.
Pursue software engineering & development courses from top universities!
The round function in Python is a built-in tool that helps you round off numbers. It simplifies a number by removing extra decimal digits. This is useful when you want clean, readable values, especially in billing, reporting, and data formatting.
By default, the round() function returns a number rounded to the nearest whole number. However, you can also tell it how many digits to keep after the decimal point.
Unlock a career at Big Four with the following courses:
The round function in Python accepts two arguments. The first one is required, while the second is optional. Depending on how you use it, the result can be either a float or an integer.
Here's the general syntax used in Python:
round(number, ndigits)
Parameter Details:
Must Explore: Argument vs Parameter: Difference Between Argument and Parameter [With Example]
Now, let’s break this down with a simple example.
Example: Understanding round() Syntax
We will now pass two arguments to the round() function. One example will skip the second argument, and the other will include it.
# Using round() with and without the second argument
# Only one argument - rounds to nearest integer
rounded1 = round(12.76)
# Two arguments - rounds to 1 decimal place
rounded2 = round(12.76, 1)
# Print the results
print("Rounded (no ndigits):", rounded1)
print("Rounded (1 decimal place):", rounded2)
Output:
Rounded (no ndigits): 13
Rounded (1 decimal place): 12.8
Explanation:
Let’s explore various use cases of the round function in Python. These examples cover everything from basic rounding to rounding values in lists, arrays, and DataFrames.
This is a simple example of rounding float values using the round() function.
# Round float numbers with and without specifying decimal places
num1 = round(8.67) # No decimal places specified
num2 = round(8.6789, 2) # Rounds to 2 decimal places
print("Rounded (no decimal places):", num1)
print("Rounded (2 decimal places):", num2)
Output:
Rounded (no decimal places): 9
Rounded (2 decimal places): 8.68
Explanation:
Python doesn't round strings directly. We first convert the string to a float.
# Convert string to float before rounding
value = "12.786"
# Use round() after type casting
rounded_value = round(float(value), 1)
print("Rounded value:", rounded_value)
Output:
Rounded value: 12.8
Explanation:
Python’s round() also works with negative floats.
# Round a negative number
num = -5.467
result = round(num, 2)
print("Rounded negative number:", result)
Output:
Rounded negative number: -5.47
Explanation:
For more control over direction, we can use the math module.
import math
num = 4.3
# Use ceil to round up
rounded_up = math.ceil(num)
# Use floor to round down
rounded_down = math.floor(num)
print("Rounded up:", rounded_up)
print("Rounded down:", rounded_down)
Output:
Rounded up: 5
Rounded down: 4
Explanation:
Must explore the Precedence of Operators in Python article!
Let’s use math with negative numbers for more flexibility.
import math
num = -6.79
print("Ceil:", math.ceil(num))
print("Floor:", math.floor(num))
Output:
Ceil: -6
Floor: -7
Explanation:
NumPy offers more control over arrays or float values.
import numpy as np
array = [1.235, 2.678, 3.456]
# Round each number to 1 decimal place
rounded_array = np.round(array, 1)
print("Rounded array:", rounded_array)
Output:
Rounded array: [1.2 2.7 3.5]
Explanation:
You can use list comprehensions to round each item in a list.
# Original list of float values
values = [4.356, 5.678, 6.444]
# Round each value to 2 decimal places
rounded_list = [round(num, 2) for num in values]
print("Rounded list:", rounded_list)
Output:
Rounded list: [4.36, 5.68, 6.44]
Explanation:
Also read about the Identity Operator in Python to improve your coding skills!
You can round numeric columns in a Pandas DataFrame directly.
import pandas as pd
# Create a DataFrame
df = pd.DataFrame({
'Price': [13.678, 45.987, 27.456]
})
# Round the 'Price' column to 1 decimal place
df['Price'] = df['Price'].round(1)
print(df)
Output:
Price
0 13.7
1 46.0
2 27.5
Explanation:
Go through the Memory Management in Python article to speed up development time!
Python provides different rounding methods to handle various use cases. Each method serves a specific purpose depending on your need for accuracy, performance, or control over the output.
Rounding Method | Best For | Behavior |
round() | General-purpose rounding | Nearest integer or decimal |
math.floor() | Conservative rounding down | Always down |
math.ceil() | Aggressive rounding up | Always up |
numpy.round() | Numerical arrays and fast computations | Element-wise array rounding |
pandas.DataFrame.round() | Tabular data with rows/columns | Per-column rounding |
The round function in Python plays a vital role in managing numerical precision. Whether you are building a finance app or analyzing scientific data, rounding helps ensure clarity and consistency in your results.
Knowing when and how to use the right rounding method can save time and avoid errors. This is especially important when you are processing large volumes of data or presenting results to users.
The Round Function in Python is used to round a number to the nearest integer or to a specific number of decimal places. It helps format numeric outputs and is widely used in financial, scientific, and statistical applications.
If you skip the second parameter in the round() function, Python will round the number to the nearest integer. By default, it assumes zero decimal places, which means the output will be a whole number.
Yes, but not always as expected. Python uses round half to even (also known as bankers' rounding) when rounding a .5 value. So, round(2.5) gives 2 instead of 3, and round(3.5) returns
Absolutely. The Round Function in Python works with both positive and negative numbers. It rounds them based on the same logic—towards the nearest even number when at a .5 midpoint or based on the decimal precision specified.
The round() function rounds to the nearest value. In contrast, floor() always rounds down to the lower integer, and ceil() always rounds up. Use them based on whether you need nearest, upper, or lower boundary values.
While generally reliable, the round function in Python may not give exact decimal results due to floating-point precision errors. This is common in most programming languages and can affect calculations when very precise values are required.
The built-in round() function works on single values only. To round numbers in a list or array, you should use loops, list comprehensions, or libraries like NumPy, which support element-wise rounding on arrays.
NumPy provides functions like numpy.round() to round entire arrays. These are faster and more efficient for large datasets. They also let you specify the number of decimal places for all elements in a single operation.
Yes, Pandas includes a .round() method that can round all or specific columns of a DataFrame. This is especially useful when you want consistent formatting for numerical data before exporting or displaying it.
Yes. To always round up, use math.ceil(). To always round down, use math.floor(). These functions are part of Python’s math module and help in situations where precision control in one direction is needed.
Use the round function in Python for general rounding needs where you want a simple, built-in option. For more control or bulk operations on arrays and tables, go with NumPy or Pandas. Each method fits different use cases.
Take our Free Quiz on Python
Answer quick questions and assess your Python knowledge
Author|900 articles published
Previous
Next
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.