How about this

x = 3.14159265
print(f'pi = {x:.2f}')

Docs for f-strings

Answer from JBernardo on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python โ€“ Real Python
April 24, 2024 - To format a float for neat display within a Python f-string, you can use a format specifier. In its most basic form, this allows you to define the precision, or number of decimal places, the float will be displayed with.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pythonic way to show 2 decimal places in f-strings?
r/learnpython on Reddit: Pythonic way to show 2 decimal places in f-strings?
January 13, 2018 -

I've got a number that needs to be rounded to 2 decimal places. Getting the round is easy, but if the number is a whole number or only has 1 digit beyond the decimal the rounding doesn't properly show 2 decimal places.

answer = 1
print(round(float(answer),2))

>> 1.0

I really like f-strings, so I would use this:

answer = 1
print(f"{round(float(answer),2):.2f}")

>> 1.00

Is there a neater or more readable method of doing this?

๐ŸŒ
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 ...
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ fixed digits after decimal with f-string
Fixed digits after decimal with F-string - AskPython
May 31, 2023 - If we write only %f instead of any number, then it will print a float number without rounding behavior. F-string is very faster compared to other formatting techniques available in the Python language.
Top answer
1 of 16
2330

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
2 of 16
838

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
Find elsewhere
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - # Monthly subscription cost for ... the :.2f is the magic part. The colon starts the format specifier, the .2 indicates two decimal places, and the f tells Python to treat the value as a float....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
July 23, 2025 - 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.
๐ŸŒ
PW Skills
pwskills.com โ€บ blog โ€บ python โ€บ what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places Explained
October 30, 2025 - Placeholder Replacement: When you use โ€œ%.2fโ€ in a format string and apply the % operator with a float value, Python replaces โ€œ%.2fโ€ with the float value formatted to display two decimal places. Hereโ€™s an example demonstrating how you can use โ€œ%.2fโ€ in Python: ... The formatted ...
๐ŸŒ
Cjtu
cjtu.github.io โ€บ spirl โ€บ python_str-formatting.html
3.10. String Formatting (Interactive) โ€” Scientific Programming In Real Life
To print the following lines as 10 characters each, we would specify .f as the format code and Python will automatically add spaced in front to make the output 10 characters long: print(".f" % 1) print(".f" % 10) print(".f" % 100) print(".f" % 1000) print(".f" % 10000) ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-string-to-float-with-2-decimals-easy-conversion-guide
Python String to Float with 2 Decimals: Easy Conversion Guide โ€“ Be on the Right Side of Change
Hereโ€™s the syntax for a float with two decimal places: your_float = 7.326 formatted_float = f"{your_float:.2f}" print(formatted_float) # Output: 7.33 ยท F-strings are a powerful and friendly way to handle precision and formatting directly within your string literals. In this section, youโ€™ll see how to translate string representations of numbers in Python into float objects, specifically focusing on maintaining two decimal places.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python limit floats to two decimal points
Python Limit Floats to Two Decimal Points - Spark By {Examples}
May 31, 2024 - To limit a float to two decimal points using f-strings, you can include the expression inside braces ({}) and use the format string :.2f. # Using f-string formatted_num = f"{num:.2f}" print(formatted_num) # Output: 3.14 ยท Pythonโ€™s built-in ...
๐ŸŒ
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 - y = 12.9999 # Print an empty 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. print("Formatted Number: ...
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ bitgrit-data-science-publication โ€บ python-f-strings-tricks-you-should-know-7ce094a25d43
Python F-strings Tricks You Should Know | by Benedict Neo | bitgrit Data Science Publication | Medium
October 12, 2022 - Or, if you want f string to print out a percentage value, you can use :.2% telling Python to set 2 decimal places and add a percentage sign to the end of the string.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
The format string specifies the desired formatting for the value, and the {:.2f} syntax specifies that the value should be formatted as a float with two decimal points. Both round and format can be used to limit floats to a fixed number of decimal points in Python...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - GeeksforGeeks
July 23, 2025 - from decimal import Decimal, ... specifies that we want to round to two decimal places n2 = n1.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) print(n2) ......