Skip to main content
HomeTutorialsPython

How to Round to 2 Decimal Places in Python

Learn how to round a number to two decimal places in Python for improved precision using techniques like round(), format(), and string formatting techniques.
Aug 2024  · 7 min read

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 in data manipulation and transformation. You will also find the Python Cheat Sheet for Beginners useful as a quick reference if you need a refresher on Python syntax and functions.

How to Round a number to 2 Decimal Places in Python

The simplest method to round a number to two decimal places in Python is by using the built-in round() function, which is pretty straightforward. 

# Using the round() function to round a number to two decimal places
rounded_number = round(3.14159, 2)

print(rounded_number)
3.14

Build Machine Learning Skills

Elevate your machine learning skills to production level.

Start Learning for Free

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 refer 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.

Methods to Round to 2 Decimal Places in Python

Python provides different methods for rounding numbers to two decimal places. The following examples provide detailed explanations of these techniques.

Round to 2 decimal places using the round() function

The round() function is Python’s built-in function for rounding float 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.

Round to 2 decimal places using string formatting techniques

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.

Using 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

Using 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 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

Using f-strings (Python 3.6+)

The f-strings were introduced in Python 3.6, and it offers a concise and efficient way to format strings and numbers. This method improves code readability and maintainability by embedding the expressions directly inside string literals. 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 the format() function

The format() function is another good option for string formatting. The format() function can handle various data types, including floats, integers, and strings. The code below prints 345.69.

# Example number to be rounded
number = 345.68776

# Using format() to round to 2 decimal places
formatted_number = "{:.2f}".format(number)

print(formatted_number)
345.69

Round to 2 decimal places 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, apply the math.floor() function, and divide by 100. The code below prints 15.43.

# 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

Round to 2 decimals 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 round 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.

Conclusion

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 better to understand the best fit for their use cases.

If you want to advance your Python skills, check our Python Programming and Python Fundamentals skill tracks which offer a comprehensive guide to becoming a proficient programmer. 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

Master Python skills to become a machine learning scientist

Photo of Allan Ouko
Author
Allan Ouko
LinkedIn
I create articles that simplify data science and analytics, making them easy to understand and accessible.

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.

Topics

Learn Python with DataCamp

Course

Introduction to Python

4 hr
5.7M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

tutorial

How to Round Up a Number in Python

Discover three straightforward techniques to round up numbers in Python: using math.ceil() from the math module, using the decimal module, and NumPy.
Allan Ouko's photo

Allan Ouko

7 min

tutorial

Python String format() Tutorial

Learn about string formatting in Python.
DataCamp Team's photo

DataCamp Team

5 min

tutorial

How to Square a Number in Python: Basic Examples and Advanced Methods

Squaring in Python is easy: Use the in-built ** operator or try NumPy, pow(), math.pow(), bitwise operators, and other functions for more versatile solutions.
Allan Ouko's photo

Allan Ouko

11 min

tutorial

Python Tutorial for Beginners

Get a step-by-step guide on how to install Python and use it for basic data science functions.
Matthew Przybyla's photo

Matthew Przybyla

12 min

tutorial

Python Decorators Tutorial

In this tutorial, learn how to implement decorators in Python.
Derrick Mwiti's photo

Derrick Mwiti

7 min

tutorial

f-string Formatting in Python

Learn about the f-string formatting technique in Python 3.6. In this tutorial, you'll see what advantages it offers and go through some example use cases.
Hafeezul Kareem Shaik's photo

Hafeezul Kareem Shaik

5 min

See MoreSee More