Python round() is used to round a number to a whole number or decimal place. Python round() is helpful when working with calculations that make unnecessary decimal places, or with values like money which has only two decimal places.
Python includes many built-in functions that can be used to process numbers. When you’re working with numbers in Python, you may encounter a scenario where you want to round a number to a certain decimal place. You can use the
round() function in Python to do this. For example, you may be running a butcher shop and want to round the prices of your products to the nearest dollar, or you may be a data analyst wanting to round the result of a math calculation to the nearest whole number.
In this tutorial, we will explore how to round numbers using the
round() function in Python. We’ll also go through a few examples to illustrate how this function would work in an actual program.
Rounding numbers is an important function in programming. When you have a floating-point number that includes many decimal places, you may want to round it. For example, if you are presenting a number to a user, you may want it to appear as a whole number, or, if you intend on using the number for a later calculation, rounding may be required.
round() function can be used to round numbers to a specific decimal place. The function takes in two arguments: the number you want to round and the number of decimal places to which you would like your new number to be rounded. The second parameter defaults to zero, producing a whole integer with no decimal point.
Here’s the syntax for the Python
One of the most common uses of rounding numbers is when you’re working with money because many people prefer not to work with fractions of pennies. Rounding numbers is also common when you’re working with mathematical calculations that return unrounded values.
Let’s use an example to illustrate how this function works. Say that we manage a produce stand and are looking to calculate how we should price individual oranges. We buy oranges in bundles of fifty but sell single oranges to customers. So, we want to divide the price of fifty oranges by fifty to get the wholesale price of an individual orange.
Here’s an example program we could use to accomplish this task:
fifty_oranges = 15.52 individual_orange = fifty_oranges / 50 print(individual_orange) rounded_value = round(individual_orange, 2) print(rounded_value)
Our code returns a floating-point number followed by, on a new line, a rounded number:
In our program, we first declared the price of fifty oranges, which was $15.52 in this case. Then, we calculated the price of an individual orange by dividing the price of fifty oranges by fifty.
However, the price of an individual orange turned out to be $0.3104, which is not a price that we can charge consumers. So, we used the
round() function to return the price of an individual orange rounded to the nearest penny. Our program returned the rounded number 0.31, which tells us that we want to charge more than $0.31 per orange to make a profit.
In our example, we specified
2 in our
round() method, which tells our program to round our number to two decimal places. However, if we wanted the
round() function to return the nearest integer, we would not need to specify this parameter. Here’s an example of using
round() without the second parameter to round a number to its nearest whole value:
number = 24.28959 print(round(number))
Our Python code returns:
round() function is usually used with floating-point numbers, which include decimal places. You can also pass integers into
round() without returning an error. With that said, our program will return the same values because integers are already round numbers.
round() function can also be used to round a negative number. Here’s an example of this in action:
Our code returns:
You can use the Python
round() function to round a number to a certain decimal place. This can be helpful if you’re working with mathematical calculations that return numbers with unnecessary decimal places, or if you are working with values like money which typically do not show more than two decimal places.
In this tutorial, we discussed how to use the
round() function to round numbers in Python. We then explored a few examples using the
round() function to show how it works. Now you’re ready to start rounding numbers in Python like an expert!