How do you round a value to two decimal places in Python? That’s an excellent question. Luckily for you, Python offers a few functions that let you round numbers.
Rounding numbers to two decimal places is common. Money only uses two decimal places; you do not want to process a monetary value with a third decimal place. Weights are often represented with two decimal places; extra values are for additional precision that is not necessary in most cases.
In this guide, we talk about how to round a number to two decimal places in Python. We walk through two code snippets to help you learn how to round numbers in your own code.
Rounding Numbers Using round()
We have to write a program that calculates how much money each friend in a group of friends has to pay for dinner. Start by asking the user to insert how much a meal cost and how many friends were present:
meal_cost = input("How much did your meal cost? ") friends = input("How many friends were with you? ")
Next, let’s calculate how much each friend has to pay for dinner. We assume that each friend is to pay an equal amount, no matter what they ordered:
cost_per_friend = float(meal_cost) / float(friends)
We have converted both “meal_cost” and “friends” to a floating point number. This is because the input()
method returns a string. You cannot perform a mathematical calculation on a string value using Python’s math operators.
Now, let’s round this value to two decimal places. We use the round()
method. The round() method. lets you round a value to the nearest integer or the nearest decimal place.
We also print the amount each friend has to contribute toward dinner to the console:
rounded_value = round(cost_per_friend, 2) print("Each friend must pay $" + str(rounded_value) + " for dinner.")
We’ve used the built-in round()
function to round the value of “cost_per_friend” to two decimal places. Next, we’ve used string concatenation to create a message that we print to the console. This message informs us how much each friend must pay for their dinner.
Run our code to see our function working:
How much did your meal cost? 64.92 How many friends were with you? 6 Each friend must pay $10.82 for dinner.
If we had divided 64.92 by six without rounding the number, we would have seen a number with a lot of decimals. Our code calculates that each friend must pay $10.82 toward their dinner by rounding the result of our division sum to two decimal places.
Rounding Numbers Using String Formatting
In our last example, we’ve shown the rounded number in a string. There is another way that we can round a number if its only purpose is to be shown in a string. This method is only supported in Python 3.x.
We revise our code to round the value that each friend must pay for dinner using the .format()
string formatting syntax.
We start by calculating how much each friend must pay:
meal_cost = input("How much did your meal cost? ") friends = input("How many friends were with you? ") cost_per_friend = float(meal_cost) / float(friends)
Next, write a print()
statement that informs us of how much each friend must pay for dinner. We use a .format()
statement with the print()
statement. This lets us format values inside our string:
print("Each friend must pay ${:0.2f} for dinner.".format(cost_per_friend))
Our .format()
statement uses the following syntax to create a decimal representation of a number that is rounded to two places:
{:0.2f}
The colon (:) tells our code we want to format a value. The zero is used to make Python aware that we’re dealing with a number that may be padded with zeros. The .2 instructs Python to round our number to two decimal places. The “f” shows our value as a number.
Let’s run our code and see what happens:
How much did your meal cost? 64.92 How many friends were with you? 6 Each friend must pay $10.82 for dinner.
Our code successfully calculates how much each friend must pay for dinner. The result is the same as our first code snippet. There are two digits after the decimal which shows that our numeric data has been rounded to two decimal points.
This method is more efficient than our first code snippet because all we want to do is print a value to the console. In this code, we’ve not used a round()
statement.
If we intended to use our rounded value further in our code, the string formatting syntax would be impractical.
Conclusion
The most common approaches to rounding a value to two decimal places are:
- To use the round() method
- To use string formatting
The first method is good for any case where you need to round a number. The second method only works if you want to round a value for use in a string and if you are using Python 3.x.
Now you’re ready to round values to two decimal places in your code like a Python pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.