Include the type specifier in your format expression:

>>> a = 10.1234
>>> f'{a:.2f}'
'10.12'
Answer from Robᵩ on Stack Overflow
🌐
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 15, 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?

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
July 23, 2025 - It takes two parameters, the number to be rounded off, and an optional parameter, the number up to which the given number is to be rounded. 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.
🌐
freeCodeCamp
freecodecamp.org › news › 2f-in-python-what-does-it-mean
%.2f in Python – What does it Mean?
June 22, 2022 - So %.2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.
🌐
Codecademy
codecademy.com › article › rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
This method allows us to include variables and expressions directly within the string literals by prefixing the string with the letter f or F. F-strings simplify creating readable strings that incorporate expressions and values directly in line. The below syntax can be used for formatting floating-point numbers to two decimal places in Python: ... Here f stands for floating-point format and .2 specifies that we want to round the number to 2 decimal places.
🌐
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 - However, modern formatting methods ... format a floating-point number with two decimal places in Python, you can use the .2f format specifier....
🌐
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.
🌐
AskPython
askpython.com › home › fixed digits after decimal with f-string
Fixed digits after decimal with F-string - AskPython
May 31, 2023 - Here the number is rounded up to 2 digits after decimal points. ... This rounding behavior of the %f format method can be controlled by the change in syntax. 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.
Find elsewhere
🌐
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) ... Below we use the % operator after the string to include the three elements into the three locations denoted by % within the string. We use three different format codes for the three numbers included in the string: ... The float format code rounding to 2 decimal places .2f.
🌐
Sololearn
sololearn.com › en › Discuss › 3288381 › how-to-round-to-2-decimal-places-in-python
How to round to 2 decimal places in python | Sololearn: Learn to code for FREE!
I have amended it to this: num =int(input()) kal = 500 def discount(x): if x > 1 : x = (x*0.9) return x dis_num = discount(num) total_kal = (dis_num*kal) def tax(x): x = (x*1.07) return x final_price = (tax(total_kal)/100) y = round(final_price, 2) ...
🌐
DZone
dzone.com › coding › languages › python f-strings
Python F-Strings
August 22, 2023 - To format a number as a percentage using F-strings, simply include the number inside the curly braces, followed by a colon and the % symbol: ... x = 0.75 print(f"{x:.2%} of the time, it works every time.") #Output 75.00% of the time, it works ...
🌐
Replit
replit.com › home › discover › how to round to 2 decimal places in python
How to round to 2 decimal places in Python
The f tells Python to treat the number as a float. The .2 limits the output to two decimal places. This method is purely for display and returns a new string, leaving the original number's value intact.
🌐
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
🌐
DataCamp
datacamp.com › tutorial › python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - Learn how to round a number to two decimal places in Python for improved precision using techniques like round(), format(), and string formatting techniques.
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
🌐
Java2Blog
java2blog.com › home › python › how to round to 2 decimals in python
How to round to 2 decimals in Python - Java2Blog
May 21, 2021 - We can round up numbers using this function by specifying the string format as {.2f}. In this format, the f is used to indicate a floating value, and .2 specifies that we require the value to have 2 decimal places.
🌐
Intellipaat
intellipaat.com › home › blog › how to round-floating value to two decimal places in python
How to Round-Floating Value to Two Decimal Places in Python
July 25, 2025 - This method was introduced in Python 3.6. ... Code Copied! ... Explanation: The code here uses :.2f inside the f-string to round and convert the float value to a string with two decimal places.