You should use the new format specifications to define how your value should be represented:

>>> from math import pi  # pi ~ 3.141592653589793
>>> '{0:.2f}'.format(pi)
'3.14'

The documentation can be a bit obtuse at times, so I recommend the following, easier readable references:

  • the Python String Format Cookbook: shows examples of the new-style .format() string formatting
  • pyformat.info: compares the old-style % string formatting with the new-style .format() string formatting

Python 3.6 introduced literal string interpolation (also known as f-strings) so now you can write the above even more succinct as:

>>> f'{pi:.2f}'
'3.14'
Answer from BioGeek on Stack Overflow
🌐
Codecademy
codecademy.com β€Ί article β€Ί rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
In Python, the % operator, also called modulus operator, We can use this operator for string formatting to round values to a specific number of decimal places. Known as old-style or printf-style formatting, this approach uses format specifiers ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί precision-handling-python
Precision Handling in Python - GeeksforGeeks
August 9, 2024 - Using format() - This is yet another way to format the string for setting precision. Using round(x,n) - This function takes 2 arguments, number, and the number till which we want the decimal part rounded.
🌐
Python Guides
pythonguides.com β€Ί python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - They were introduced in Python 3.6 and have completely changed how I write code. They are fast, readable, and allow you to format numbers directly within the string. # Monthly subscription cost for a streaming service in the USA monthly_fee = 14.991234 # Using an f-string to format to 2 decimal places formatted_fee = f"Your monthly subscription is: ${monthly_fee:.2f}" print(formatted_fee) # Output: Your monthly subscription is: $14.99
🌐
Raspberry Pi Forums
forums.raspberrypi.com β€Ί board index β€Ί hardware and peripherals β€Ί raspberry pi pico β€Ί micropython
Formatting decimal places from sensor values in micropython - Raspberry Pi Forums
March 7, 2023 - bme = bme280.BME280(i2c=i2c) temp = bme.values[0] print(bme.values) print(temp) #temp = "17.46645466536C" temp_removeC = temp[:-1] ##this 'strips' the last character off the end, -3 for three characters print(temp_removeC) temperature = float(temp_removeC) ##//this makes the string a decimal point number temperature_formatted_string = "{:.2f}".format(temperature) ##//this sets the decimal places, but is now a string again print(temperature_formatted_string) #temp = "17.466536C" temperature_formatted_string = "{:.1f}".format( float(temp[:-1])) print(temperature_formatted_string + "C" ) ('17.57C', '989.76hPa', '44.65%') 17.58C 17.58 17.58 17.6C (I added the C back, and reduced decimals to one place.)
Find elsewhere
🌐
AskPython
askpython.com β€Ί python β€Ί built-in-methods β€Ί format-2-decimal-places
How to Format a Number to 2 Decimal Places in Python? - AskPython
February 27, 2023 - In this article, we learn how to format a number to 2-decimal places by using two ways. You can show the result using %f formatted, written inside quotation marks, and the % modulo operator separates it from the float number β€œ%f” % num.
🌐
w3resource
w3resource.com β€Ί python-exercises β€Ί string β€Ί python-data-type-string-exercise-30.php
Python: Print the following floating numbers upto 2 decimal places - w3resource
June 12, 2025 - # Define a variable 'x' and assign ... line for spacing. print() # Print the original value of 'x' with a label. print("Original Number: ", x) # Format the value of 'x' to two decimal places and print it with a label. ...
🌐
W3Schools
w3schools.com β€Ί c β€Ί c_data_types_dec.php
C Data Types Decimal Precision
float myFloatNum = 3.5; printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point printf("%.1f\n", myFloatNum); // Only show 1 digit printf("%.2f\n", myFloatNum); // Only show 2 digits printf("%.4f", myFloatNum); // Only ...
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί 2f-in-python-what-does-it-mean
%.2f in Python – What does it Mean?
June 22, 2022 - floatNumber = 1.9876 print("%.1f" % floatNumber) # 2.0 Β· In the code above, we added .1 between % and f in the %f operator. This means that we want the number to be rounded up to one decimal place.
🌐
Python Guides
pythonguides.com β€Ί round-numbers-to-2-decimal-places-in-python
How To Round Numbers To 2 Decimal Places In Python?
January 16, 2025 - In this example, we create a Decimal object from a string and use the quantize() method to round it to 2 decimal places. The rounding parameter is set to ROUND_HALF_UP , which rounds numbers ending in 5 up (away from zero).
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
December 16, 2024 - Now let us see different ways to get two decimal places in Python one by one. ... The f-string in Python is used to format a string. It can also be used to get 2 decimal places of a float value.
🌐
CodeGenes
codegenes.net β€Ί blog β€Ί how-to-print-2-decimal-places-in-python
Printing 2 Decimal Places in Python: A Comprehensive Guide β€” codegenes.net
Use rounding when appropriate, ... in Python can be achieved using various methods, including the format() method, f - strings, and the round() function....
🌐
EyeHunts
tutorial.eyehunts.com β€Ί home β€Ί how to display 2 decimal places in python | example code
How to display 2 decimal places in Python | Example code
January 11, 2023 - Simple example code use str.format(number) with β€œ{:.2f}” as a string and float as a number to return a string representation of the number with two decimal places. fnum = 7.154327 res = "{:.2f}".format(fnum) print(res) Output: Answer: Use ...
🌐
The Teclado Blog
blog.teclado.com β€Ί python-formatting-numbers-for-printing
Formatting Numbers for Printing in Python - The Teclado Blog
September 2, 2019 - Learn how to leverage Python's detailed formatting language to control how numbers are displayed in your applications.
🌐
Analytics Vidhya
analyticsvidhya.com β€Ί home β€Ί 6 ways to round floating value to two decimals in python
6 Ways to Round Floating Value to Two Decimals in Python
November 4, 2024 - By applying the .2f format specifier within the format() method and using the "{:.2f}" format string, the number is formatted to have two decimal places. The resulting formatted_number is then printed, which outputs 3.14 ...
🌐
Javatpoint
javatpoint.com β€Ί how-to-get-2-decimal-places-in-python
How to Get 2 Decimal Places in Python - Javatpoint
How to Get 2 Decimal Places in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.