Course
Rounding numbers to two decimal places is an important technique in Python. It is particularly useful in financial calculations, data presentation, and scientific reporting.
Python provides a range of methods for precise rounding. In this tutorial, I will show you how to round a number to two decimal places in Python using built-in functions, libraries, and formatting methods.
If you are getting started as a data analyst, I recommend taking DataCamp’s Introduction to Python course to learn the basics of Python for data manipulation and transformation. You will also find our Python NumPy tutorial useful if you need to round large arrays of numbers in data analysis workflows.
TL;DR
-
Use
round(number, 2)for arithmetic rounding — it changes the actual value. -
Use
f"{number:.2f}"orstr.format()for display only — the original float is unchanged. -
Use the
decimalmodule when exact decimal precision is required (e.g., financial calculations). -
Use
np.round(array, 2)to round an entire NumPy array in one call. -
Watch for float precision surprises:
round(2.675, 2)returns2.67, not2.68, due to how floats are stored in memory.
Build Machine Learning Skills
Understanding Rounding and Decimal Places
Decimal places refer to the numbers that appear right after the decimal point in a number. The decimal places are important in a number as they determine its precision. The more decimal places, the more precise it is, and vice versa.
Rounding numbers refers to adjusting a number by reducing the number of decimal places. This technique is usually applied to simplify a number while maintaining consistency across calculations. Still, rounding a number to specific decimal places affects the accuracy of the calculated values due to the introduction of small errors during rounding.
Python provides different methods for rounding numbers to two decimal places. The following examples provide detailed explanations of these techniques.
Using round() to Round a Number in Python
The round() function is Python’s built-in function for rounding floating-point numbers to the specified number of decimal places. You can specify the number of decimal places to round by providing a value in the second argument. The example below prints 34.15.
# Example number to be rounded
number = 34.14559
# Rounding the number to 2 decimal places
rounded_number = round(number, 2)
print(rounded_number)
# 34.15
Python's round() function uses "round half to even" as its default rounding mode when you omit the second argument. Rounding half to even (bankers’ rounding) is when a number is exactly halfway between two integers, it is rounded to the nearest even integer. This technique is useful since it can help minimize cumulative rounding errors.
Float precision gotcha with round()
There's one subtlety worth knowing before you rely on round() in financial code. Try this:
print(round(2.675, 2)) # You might expect 2.68
# Output: 2.67
The result is 2.67, not 2.68. The value 2.675 cannot be represented exactly in binary floating-point (IEEE 754); the stored value is slightly less than 2.675, so Python rounds down.
If you need exact rounding for financial calculations, use the decimal module with an explicit rounding mode:
from decimal import Decimal, ROUND_HALF_UP
result = Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(result) # 2.68
Using String Formation Techniques to Round a Number in Python
String formatting techniques are useful for rounding numbers to two decimal places, especially when displaying the number in the output.
Keep in mind that when you use string formatting techniques in Python to round a number, the rounded output is displayed as a string, but the original number remains unchanged. If you do math on the original number, the calculations are based on the unrounded value, which can lead to surprising results.
Rounding with the % operator
The % operator offers a traditional method of formatting numbers to two decimal places. The % operator allows for creating formatted strings by inserting values into placeholders.
In the example below, f indicates the value as a floating-point number while .2 specifies the decimal places to round the number.
# Example number to be rounded
number = 3.14159
# Using the % operator to round to 2 decimal places
formatted_number = "%.2f" % number
print(formatted_number)
# 3.14
Rounding with str.format()
The str.format() method provides a more flexible way to handle complex rounding techniques. Because it uses named placeholders, developers tend to prefer this method over the % operator. In the example below, :.2f is used within the curly brackets to specify that the number is rounded to two decimal places. The code will print 3.14.
# Example number to be rounded
number = 3.14159
# Using str.format() to round to 2 decimal places
formatted_number = "{:.2f}".format(number)
print(formatted_number)
# 3.14
Rounding with f-strings (Python 3.6+)
F-strings arrived in Python 3.6 and are now the preferred way to embed values in strings. The f-string syntax is especially clean for rounding: you get display formatting in one line without a separate call. The code below prints 14.68.
# Example number to be rounded
number = 14.67856
# Using f-strings to round to 2 decimal places
formatted_number = f"{number:.2f}"
print(formatted_number)
# 14.68
Using format() to Round a Number in Python
The built-in format() function takes a value and a format spec and returns a formatted string. Unlike str.format(), you pass the number directly rather than embedding it in a template string. The code below prints 345.69.
# Example number to be rounded
number = 345.68776
# Using the built-in format() to round to 2 decimal places
formatted_number = format(number, ".2f")
print(formatted_number)
# 345.69
Using Other Modules to Round a Number in Python
Beyond basic Python, there are many other modules you can use to round numbers in Python. I'll show you the three most prominent examples: math, decimal, and NumPy.
Rounding using the math module
The math module does not provide functions that directly round off numbers to specific decimal places. However, you can combine the math module and other arithmetic to round a number to two decimal places.
The math.floor() function is used to round down a number to the nearest integer. To round down a number to two decimal places, you multiply it by 100 and apply the math.floor() function, and divide by 100. The code below prints 3.14.
# Import math module
import math
# Example number to be rounded
number = 3.14159
# Using math.floor() to round down to 2 decimal places
rounded_down = math.floor(number * 100) / 100
print(rounded_down)
# 3.14
Similarly, the math.ceil() function rounds up a number to the nearest integer. To round up a number to two decimal places, multiply it by 100, apply the math.ceil() function, and divide by 100. The code below prints 3.15.
# Import the math module
import math
# Example number to be rounded
number = 3.14159
# Using math.ceil() to round up to 2 decimal places
rounded_up = math.ceil(number * 100) / 100
print(rounded_up)
# 3.15
Rounding using the decimal module
The decimal module in Python is useful for rounding float numbers to precise decimal places using the .quantize() method. In the example below, we set the precision as 0.01 to indicate we want to round the number to two decimal places.
# Import the decimal module
from decimal import Decimal
# Example number to be rounded
number = Decimal("18.73869")
# Define the rounding precision to 2 decimal places
precision = Decimal('0.01')
# Using the quantize method with ROUND_UP
# to round the number up to 2 decimal places
rounded_number = number.quantize(precision)
print(rounded_number)
# 18.74
If you are looking for specific rounding-up behavior, check out our latest tutorial on How to Round Up a Number in Python to learn more about using the math and decimal modules, and other techniques to make sure the number always rounds up, as opposed to down. If you want to understand more data transformation more generally, go through our Data Analyst with Python career track to grow your analytical skills.
Rounding using NumPy
When working with arrays in NumPy, use np.round() to round every element at once. This is faster and more readable than looping over values manually.
import numpy as np
prices = np.array([1.2345, 9.8765, 3.14159])
rounded_prices = np.round(prices, 2)
print(rounded_prices)
# [1.23 9.88 3.14]
np.round() applies the same banker's rounding rule as Python's built-in round(). The .round() method works the same way on pandas DataFrames and Series:
import pandas as pd
df = pd.DataFrame({"price": [1.2345, 9.8765, 3.14159]})
df["price_rounded"] = df["price"].round(2)
print(df)
# price price_rounded
# 0 1.2345 1.23
# 1 9.8765 9.88
# 2 3.14159 3.14
For a broader look at data transformation with pandas, see our pandas tutorial.
When to Use Each Python Rounding Method
Here's a quick reference for choosing the right rounding approach:
| Method | Best for | Changes value? | Returns |
|---|---|---|---|
round(x, 2) |
General arithmetic | Yes | float |
f"{x:.2f}" |
Display / print | No | str |
str.format() |
Display / print | No | str |
% operator |
Display / print (legacy) | No | str |
format(x, ".2f") |
Display / single value | No | str |
math.floor() / ceil() |
Always round down / up | Yes | float |
Decimal.quantize() |
Financial / exact decimal | Yes | Decimal |
np.round(arr, 2) |
NumPy arrays | Yes | ndarray |
A few rules of thumb to guide the choice:
-
Just displaying a number? Use an f-string (
f"{x:.2f}"). One line, no imports, no side effects on the original value. -
Doing arithmetic with the result? Use
round(x, 2). It returns a float you can keep calculating with. -
Financial or accounting code? Use
Decimal.quantize(). Float precision surprises (round(2.675, 2) == 2.67) don't happen with thedecimalmodule. -
Need to always go up or always go down? Use
math.ceil()ormath.floor(). They ignore the rounding rules entirely. -
Working with a NumPy array or pandas column? Use
np.round(arr, 2)orseries.round(2). Both handle the whole collection at once. -
Maintaining old code? The
%operator still works, but f-strings replaced it in Python 3.6 — prefer those in new code.
Final thoughts
Rounding numbers to two decimal places is an important technique for improved precision in financial and scientific calculations. In this article, we have discussed the different methods of rounding numbers to two decimal places, including built-in functions, string formatting techniques, and the math module. It is important to understand and select the appropriate method based on the specific requirements, such as precision and formatting style. I encourage you to practice the different methods using different examples to better understand the best fit for their use cases.
If you want to advance your Python skills, check our Python Programming skill track, which covers functions, decorators, and advanced Python patterns. Our Python Developer career track is also designed to help you upskill as a developer while learning more advanced data structures and algorithms.
Become a ML Scientist
Frequently Asked Questions
What is the simplest way to round a number to two decimal places in Python?
The simplest way to round a number to two decimal places is by using the built-in round() function.
Why does a number round to the nearest integer when using the round() function?
The round() function default method is round half to even. To round a number to two decimal places, you must provide the second argument as 2 in the function, i.e., round(3.14159, 2).
How is the format() function used to round a number to two decimal places?
The format() function is used to round a number to the specified number of decimal places and display it within a formatted string.
What is round half to even?
The rounding half to even method or bankers’ rounding involves rounding a number exactly halfway between two integers to the nearest even integer.
Does the math module provide rounding a number to two decimal places in Python?
You can only use the math.floor() and math.ceil() functions with other arithmetics to round a number to a specified number of decimal places.
Why does round(2.675, 2) return 2.67 instead of 2.68?
This is a floating-point precision issue. The value 2.675 cannot be represented exactly in binary floating-point (IEEE 754); the stored value is slightly less than 2.675, so Python rounds down to 2.67.
To avoid this in financial code, use the decimal module with an explicit rounding mode: Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) returns 2.68 as expected.
How do I round all values in a pandas DataFrame to 2 decimal places?
Use the .round() method on a DataFrame or Series. To round a single column: df["price"] = df["price"].round(2). To round all numeric columns at once: df = df.round(2).



