Being able to work with and round floating point values, or decimal values, in Python is an important skill. In this tutorial, you’ll learn how to round to decimal places in Python, including learning how to round up or down. You will also learn how to simply represent values with 2 decimal places, without changing the value itself.
By the end of this tutorial, you’ll have learned:
- How to round to 2 decimal places in Python, including rounding up and down
- How to format values as currency in Python
- How to round lists and arrays of numbers to 2 decimal places in Python
Table of Contents
How to Round to 2 Decimal Places in Python
Python provides a function, round()
, which is used to round values to a specific precision level. The function takes two parameters: the value to round and the precision to use. Let’s take a look at what the function looks like and how we can use it:
# Understanding the Python round() Function
round(number[, ndigits])
We can see that there are two parameters, one of which is required:
number=
, represents the number to roundndigits=
, represents the number of digits to use. IfNone
, the number is rounded to its nearest integer.
Let’s see how we can use the Python round()
function to round a float to 2 decimal places:
# Rounding a Value to 2 Decimal Places in Python
value = 1.2345
rounded = round(value, 2)
print(rounded)
# Returns: 1.23
In the following section, you’ll learn how to round up a float to 2 decimal places.
How to Round Up to 2 Decimal Places in Python
By default, Python will round values that are greater than 5 up, and less than or equal to 5 down. In order to round a value up, you need to use a custom process. This is often done in finance or accounting, where values are rounded up, regardless of their value. For example, $3.221 would be rounded to $3.23.
Python provides another function, ceil()
, which allows you to round values up to their nearest integer. We can use this function to round values up to 2 decimal places:
# Rounding Up to 2 Decimal Places
import math
value = 1.2121
rounded = math.ceil(value * 100) / 100
print(rounded)
# Returns: 1.22
Let’s break down what the code above is doing:
- We multiply our number by 100
- We pass the value into the
ceil()
function - We then divide the number by 100, to bring it back to 2 decimal places
We can use a similar process to round floating point values down to 2 decimal places.
How to Round Down to 2 Decimal Places in Python
Similar to the ceil()
function, Python also provides a floor()
function. This function allows us to round values down, regardless of rounding rules. We can use a process similar to the one above to round values down to two decimal places. Let’s see what this looks like:
# Rounding Down to 2 Decimal Places
import math
value = 1.2155
rounded = math.floor(value * 100) / 100
print(rounded)
# Returns: 1.21
- We multiply our number by 100
- We pass the value into the
floor()
function - We then divide the number by 100, to bring it back to 2 decimal places
In the following section, you’ll learn how to represent values to 2 decimal places without changing the underlying value.
How to Format Values as Currency in Python
In some cases, you’ll want to display values to a certain precision without changing the actual value. For example, you may want to round values to two decimal places and show them as currency. Python makes this incredibly easy using string formatting. Because string formatting is a huge topic, I have provided a full guide on this.
Let’s see how we can use f-string formatting to format a decimal as currency in Python:
# Formatting Values as Currency in Python
value = 1.23333
print(f'${value:.2f}')
# Returns: $1.23
We can see that in order to accomplish this, we only needed to apply the :.2f
formatter to the string and precede the value with a $ sign.
Want to learn how to round values as currency in a Pandas DataFrame? Check out this guide to rounding values in Pandas, including rounding values as currency.
How to Round a List of Values to 2 Decimal Places in Python
In this section, you’ll learn how to round a list of values to 2 decimal places in Python. This can be done easily using a for loop. Using a for loop, we can iterate over each item in a list and apply a transformation to it. Let’s see how we can use Python to accomplish this:
# Using a For Loop To Round a List of Values
values = [1.222, 1.5555, 3.234]
rounded = []
for value in values:
rounded.append(round(value, 2))
print(rounded)
# Returns:
# [1.22, 1.56, 3.23]
Let’s break down what we did in the code above:
- We defined two lists, one which contains our values and an empty list to hold our rounded values
- We then loop over each item in the list and pass it into the
round()
function - The rounded value is then appended to our holder list
We can also use Python list comprehensions to simplify this process quite a bit. List comprehensions allow us to iterate over lists without explicitly using a for loop. Let’s see what this looks like:
# Using a List Comprehension to Round Values to 2 Decimal Places
values = [1.222, 1.5555, 3.234]
rounded = [round(value, 2) for value in values]
print(rounded)
# Returns:
# [1.22, 1.56, 3.23]
In the following section, you’ll learn how to round all values in a NumPy array to 2 decimal places.
How to Round a NumPy Array to 2 Decimal Places in Python
When working with NumPy arrays, you may also want to round all the values in the array to 2 decimal places. Rather than using the Python round()
function, NumPy provides its own round()
function. What’s great about this function is that it allows you to pass in an entire array. This has the benefit of making the code more explicit and easier to read.
Let’s see how we can use NumPy to round an entire array to 2 decimal places:
# Round a NumPy Array to 2 Decimal Places
import numpy as np
arr = np.array([2.312, 3.1234, 4.5555])
rounded = np.round(arr, 2)
print(rounded)
# Returns:
# [2.31 3.12 4.56]
In the code above, we:
- Created a new array,
arr
, which contains our original values - We then created another array,
rounded
, which is the result of passing the array into thenp.round()
function
Conclusion
In this tutorial, you learned how to use Python to round a value to 2 decimal places. This is such a common task that it warrants its own tutorial. You first learned how to use the Python round()
function to round values to a specific precision point. Then, you learned how to customize the behavior of your programs to round up or down, regardless of rounding rules.
Then, you learned how to display values in currency format in Python. From there, you learned how to round values in Python lists and NumPy arrays.
Additional Resources
To learn more about related topics, check out the tutorials below: