Python round() Function

Python round() Function

The round() function in Python is a built-in method used to simplify numerical values by rounding them to a specified number of decimal places. If no decimal place is provided, it automatically rounds the number to the nearest whole number. For example, round(3.6) returns 4. This function is useful for producing cleaner and more readable results when working with numbers. In this article, you will understand the Python round() function in detail with examples and use cases.

Table of Contents:

What is the round() Function in Python?

The round function in Python is a useful tool that helps make numbers easier to read and work with. It takes two inputs to perform its task. The first is the number you want to round. The second, which is optional, lets you choose how many decimal places to round to. If you do not give it the number of decimal places, it will automatically round the number to the nearest whole number. If you do not give it the number of decimal places, it will automatically round the number to the nearest whole number.

Syntax of the round() Function in Python

The round() is a Python function that is simple to use and helps you round numbers easily in your programs. Its syntax is clear, so you can quickly use it whenever needed. Below is the standard way to write and use the round() function in Python.

Syntax of round()

round(number, number of digits)

Example:

Python

Output:

Syntax of the round Function in Python output

Explanation:
Here, the program takes the number 3.5678 and rounds it to two decimal places using the round() function. It then prints the result, which is 3.57.

Python Pro: Start Your Coding Journey Today!
Gain hands-on experience with Python and turn your ideas into fully functioning programs.
quiz-icon

Parameters of round() Function in Python

The round() function in Python helps you make numbers simpler and easier to read. It has two main parameters: a number and the number of digits. Each one has a different job in telling Python how to round your number.

1. Number(required): This is the number you want to round. It can be a whole number like 15 or a decimal like 3.5678.
2. Number of digits (optional): This is how many decimal places you want to keep after rounding. You can leave this out if you just want to round to the nearest whole number.

If you don’t include the second parameter, here’s what happens:

  • If you give it a whole number like 15, it will just stay 15 because it’s already rounded.
  • If you pass a decimal value like 3.5678 to the round() function, it will round it to the nearest whole number, which is 4.

Python round() Function with Examples

The round() function in Python is a straightforward yet powerful tool used to round numerical values. It takes two arguments: the number to be rounded and the number of decimal places to round to. If the second argument is not provided, Python rounds the value to the nearest whole number. The function uses conventional rounding rules, making it easy to produce accurate and readable results.

Example:

Python

Output:

Python round() Function with Examples - output

Explanation: Here, round() is applied to the number 18.07 to round it to the nearest whole number. Since no decimal places are specified, the function returns 18 as the result.

1. Python round() Function Without the Second Parameter

The round() is a built-in function in Python that helps you quickly round numbers to the nearest whole number. If you don’t provide the second parameter (which tells it how many decimal places to keep), Python just rounds your number to the closest integer. This is very handy when you want simple, whole-number results.

Example:

Python

Output:

Python round Function Without the Second Parameter output

Explanation: Here, the program first takes the integer 22 and uses the round() function to round it, but since it’s already a whole number, it remains 22. Then, the program takes the floating-point number 19.8 and rounds it to the nearest whole number. Because .8 is greater than .5, Python rounds it up to 20.

2. Python round() Function with the Second Parameter

The round() function in Python can also take a second number (called the second parameter). This extra number tells Python how many decimal places you want to round your original number to. It’s really helpful if you want your result to have a certain number of digits after the decimal point.

Example:

Python

Output:

Python round() Function with the Second Parameter - output

Explanation: Here, the program first shows how round() can keep two decimal places for 5.438, turning it into 5.44. Then, it does the same with three and one decimal places for the other numbers.

Get 100% Hike!

Master Most in Demand Skills Now!

Python round() Function with Negative Numbers

The round() function in Python works the same way for negative numbers as it does for positive ones. You can use it to round to the nearest whole number or choose exactly how many decimal places you want to keep. This is useful when you’re working with negative data, like temperatures below zero or financial numbers showing a loss.

Example:

Python

Output:

Python round() Function with Negative Numbers - output

Explanation: Here, round() is first used to round –5.55 to the nearest whole number, resulting in –6. Then, it rounds –12.65 to one decimal place, producing –12.6, and –9.8765 to two decimal places, giving –9.88. This demonstrates how the function adjusts precision based on the specified number of decimal places.

Rounding Numbers with the Math Library in Python

In Python, if you want more control over how you round numbers, like whether you always want to round up or down, you can use the math library. This library has special functions that help you do exactly that. Let’s take a look at how to round numbers up and down using the math library.

1. Rounding Numbers Up in Python

To consistently round numbers up, you can use math.ceil() function from Python’s math module. This function returns the smallest integer greater than or equal to the input, effectively rounding the number up to the nearest whole number regardless of the decimal portion.

Example:

Python

Output:

How to Round Numbers Up in Python - output

Explanation: Here, math.ceil() rounds 3.2 up to 4. Regardless of how small the decimal portion is, it always rounds the value up to the next highest whole number.

2. Rounding Numbers Down in Python

To consistently round numbers down, you can use math.floor() function. It returns the largest integer less than or equal to the given number, effectively rounding the value down to the nearest whole number.

Example:

Python

Output:

How to Round Numbers Down in Python - output

Explanation: Here, math.floor() rounds 3.8 down to 3. Regardless of the decimal part, it always returns the nearest lower whole number.

Using NumPy to Round Numbers in Python

If you’re working with lots of numbers in Python, the NumPy library can help. It makes it easier to handle large amounts of data and do different math tasks. NumPy also has helpful functions for rounding numbers in various ways. Let’s see how it works.

Example:

Python

Output:

How to Round Numbers Normally with NumPy - output

Explanation: Here, np.round() is applied to the number 3.567 to round it to two decimal places. The result is 3.57, which makes the number simpler to read and use in calculations or displays.

1. Rounding Numbers Up Using NumPy in Python

To round numbers up in Python, the numpy.ceil() function offers a reliable way to return the smallest integer greater than or equal to a given number. It returns the smallest integer that is greater than or equal to the input value. This function supports both individual numbers and arrays, and it always returns a float, even when the input is already an integer

Example:

Python

Output:

How to Round Numbers Up with NumPy - output

Explanation: Here, np.ceil() rounds 3.2 up to 4.0. Even if the decimal part is very small, it still rounds the number up to the next whole number.

2. Rounding Numbers Down Using NumPy in Python

To consistently round numbers down in Python, the numpy.floor() function can be used. This function returns the greatest integer that is less than or equal to the given input. It effectively rounds the number down to the nearest whole number, regardless of the decimal part.

Example

Python

Output:

How to Round Numbers Down with NumPy - output

Explanation: Here, the np.floor() is used to round 3.8 down to 3.0. It always goes down to the nearest whole number.

Difference Between round(), math.floor(), and math.ceil()

Feature round() math.floor() math.ceil()
Purpose Rounds a number to the nearest whole number or to a specified number of decimal places Always rounds down to the nearest whole number Always rounds up to the nearest whole number
Decimal Place Support Yes, you can specify the number of decimal places No, only works with integers No, only works with integers
Type of Result Returns a float or integer (immutable) Returns an integer (immutable) Returns an integer (immutable)
Module Requirement Built-in, no import needed Requires `math` module Requires `math` module

Best Practices for Rounding Numbers in Python

1. Define the Required Precision
Before rounding any number, clearly determine the level of precision needed. For example, when working with currency, it is common to round to two decimal places to represent cents accurately.

2. Use Python’s Built-in round() Function
The round() function is a built-in feature in Python, making it the most straightforward method for rounding numbers. It is concise, intuitive, and performs as expected in most scenarios.

3. Understand the Behavior with a Single Argument
When using round() with a single argument, Python rounds the number to the nearest whole number. To specify the number of decimal places, pass a second argument (e.g., round(3.7, 1) rounds to one decimal place).

4. Be Aware of Data Alteration
While rounding simplifies numbers for readability, it can also slightly alter their original values. Use rounding only when necessary to avoid unintended loss of accuracy.

5. Validate with Test Cases
Always test your code with multiple examples to observe how the rounding behaves. This helps identify any unexpected results early in the development process.

6. Maintain Readable and Organized Code
Write clean, readable code with meaningful variable names. Clear structure and comments, where needed, will make your code easier to understand and maintain.

Common Errors and Exceptions in Python round() Function

1. TypeError when no number is provided
The round() function needs at least one number to round. If you do not provide a number, Python will raise a TypeError because it does not know what to round.

2. TypeError if the argument is not a number
If you try to round something like a list or a string that does not represent a number, you will get a TypeError.

3. TypeError with non-integer decimal places
The second argument in round() (which tells it how many decimal places to round to) must be an integer. If you provide a decimal or something else, Python will raise an error.

4. Unexpected results with very big or small numbers
When working with very large or very small numbers, rounding can sometimes lead to unexpected results because of how computers handle numbers.

5. Understanding rounding rules
Python’s round() function uses a method called “round half to even” for numbers that are exactly in the middle, like 2.5. This means round(2.5) becomes 2 instead of 3, which can be surprising if you are not expecting it.

Real-World Use Cases of round() Function in Python

1. Money and prices
When dealing with money, you usually round numbers to two decimal places. For example, prices, bills, and taxes often need to be rounded to cents.

2. Scientific or technical results

In science or engineering, you might get very precise numbers that are hard to read. You can round these to a few decimal places to make them clearer and easier to work with.

3. Data summaries and charts
When showing data in a graph or table, rounding can make the numbers easier for others to read and understand.

4. School projects or coding tasks
If you’re writing code for school, rounding makes your results easier to read and understand. It keeps numbers clear and to the point.

5. Everyday calculations
Even in simple math problems or when you’re figuring out averages or percentages, rounding helps you get a clean answer that’s easier to explain.

Master Python Basics – 100% Free Learning Path!
Explore variables, loops, functions, and more with free Python tutorials designed for beginners.
quiz-icon

Conclusion

In this article, we explored the Python round() function and its role in simplifying numerical values for better readability and presentation. We discussed its basic usage, such as rounding to whole numbers or a specific number of decimal places, along with practical examples involving financial and scientific data. Understanding how to use the round() function effectively and avoiding common pitfalls can help you write more accurate and professional Python code. Mastering this function is a small but valuable step toward becoming a more proficient Python developer.

Take your skills to the next level by enrolling in the Python Course today and gaining hands-on experience. Also, prepare for job interviews with Python Interview Questions prepared by industry experts.

Python range() Function – FAQs

Q1. What does round(3.14159, 2) return?

It returns the number 3.14, rounding the original number to 2 decimal places.

Q2. Can round() be used without specifying the number of decimals?

Yes. If you use round(number) with just one argument, it rounds to the nearest integer.

Q3. How does round() handle .5 cases?

When rounding halfway cases like 2.5, Python uses the round-half-to-even rule (also known as banker’s rounding). So round(2.5) returns 2 and round(3.5) returns 4.

Q4. Can round() work with negative decimal places?

Yes. Using a negative decimal value (like round(1234, -2)) rounds to the nearest hundred (result: 1200).

Q5. What type does round() return?

round() returns an int if no decimal places are specified (or 0 is specified); otherwise, it returns a float.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

EPGC Data Science Artificial Intelligence