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 8, 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.
Discussions

python - Fixed digits after decimal with f-strings - Stack Overflow
Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's say I want to display 2 digits after the decimal place. More on stackoverflow.com
🌐 stackoverflow.com
python - f-string decimal formatting rounding - Stack Overflow
Other questions have indicated that Python f-strings round digits at a certain decimal place, as opposed to truncating them. ... >>> number = 3.1415926 >>> print(f"The number rounded to two decimal places is {number:.2f}") The number rounded to two decimal places is 3.14 >>> >>> number2 = ... More on stackoverflow.com
🌐 stackoverflow.com
python - Limiting floats to two decimal points - Stack Overflow
By the way, since Python 3.6 we can use f-strings: f"Result is {result:.2f}" 2019-02-20T12:54:20.13Z+00:00 ... The built-in round() works just fine in Python 2.7 or later. ... Check out the documentation. ... So am I to understand that this is a Python 2.7 fail? Why would such a fundamental function yield different results from v 2.7 to v 3? 2017-09-02T23:50:55.62Z+00:00 ... For example, if you try to round the value 2.675 to two decimal places... More on stackoverflow.com
🌐 stackoverflow.com
rounding - How to round to 2 decimals with Python? - Stack Overflow
Just use the formatting with %.2f which gives you rounding down to 2 decimals. def printC(answer): print "\nYour Celsius value is %.2f C.\n" % answer ... You can use the string formatting operator of python "%". More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
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.
🌐
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.
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.
🌐
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....
🌐
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) ...
🌐
Replit
replit.com › home › discover › how to round to 2 decimal places in python
How to round to 2 decimal places in Python
February 6, 2026 - 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.
🌐
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 ...
Top answer
1 of 16
2331

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
🌐
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.
🌐
Derludditus
derludditus.github.io › ClaudePython › how-to-round-to-2-decimal-places-in-python.html
How to round to 2 decimal places in Python
The syntax f"{number:.2f}" combines string interpolation with precise decimal control in a single, readable expression. The f prefix enables direct variable embedding while .2f controls the decimal precision. This modern approach produces identical results to format() but requires less typing ...
🌐
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.