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.
Answer from Rex Logan on Stack Overflow
๐ŸŒ
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 2 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
Discussions

python - How can I format a decimal to always show 2 decimal places? - Stack Overflow
Consider r = 1; "%s" % r; r = (1, ... now (and Python 3 deprecated the entire method of string formatting as error prone). 2010-01-04T01:06:13.513Z+00:00 ... I suppose you're probably using the Decimal() objects from the decimal module? (If you need exactly two digits of precision beyond the decimal point with arbitrarily large numbers, you definitely ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Pythonic way to show 2 decimal places in f-strings?

Do the second one, just donโ€™t worry about rounding it. Let the formatting do the rounding for display.

More on reddit.com
๐ŸŒ r/learnpython
10
1
January 17, 2018
How to format to two decimal places python
There are several ways to do it, string formatting being one, but round is the easiest: String formatting: print('%.2f' % number) round: float_number = round(number, 2) More on reddit.com
๐ŸŒ r/learnprogramming
4
1
April 29, 2021
How can I format a decimal to always show 2 decimal places in Python? - TestMu AI Community
How can I format a decimal to always show 2 decimal places in Python? I want to display values like: - 49 as 49.00 - 54.9 as 54.90 Regardless of the number of decimal places, I want to ensure that the decimal is always displayed with 2 decimal places. This is for displaying monetary values, ... More on community.testmuai.com
๐ŸŒ community.testmuai.com
0
December 2, 2024
People also ask

How can I customize decimal places with %.2f?
You can change the number before โ€œfโ€. For example, %.3f gives 3 decimal places and %.1f gives 1 decimal place. Python formats the number accordingly.
๐ŸŒ
pwskills.com
pwskills.com โ€บ blog โ€บ python โ€บ what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
What does "%.2f" mean in Python?
โ€œ%.2fโ€ tells Python to format a floating-point number with exactly 2 digits after the decimal. It rounds the value if needed and outputs a clean, consistent numeric format. This is commonly used in reports, billing systems, financial calculations, and formatted print statements.
๐ŸŒ
pwskills.com
pwskills.com โ€บ blog โ€บ python โ€บ what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
What does .2f do in Python?
โ€œ.2fโ€ specifies the precision part of a format string, ensuring the float shows only two decimal places. It applies rounding automatically and works with print(), f-strings, and format().
๐ŸŒ
pwskills.com
pwskills.com โ€บ blog โ€บ python โ€บ what does %.2f mean in python?
What Does %.2f Mean In Python? Format Float To 2 Decimal Places ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python 74: Formatting a floating point number to 2 decimal places using the format() method - YouTube
Displaying/formatting a floating point number to 2 decimal places using the format() method. .2f specifies two decimal places, the number is displayed round...
Published ย  October 15, 2024
๐ŸŒ
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 % operator can also be used to get two decimal places in Python. It is a more traditional approach which makes the use of string formatting technique. ... Python math module provides various function that can be used to perform various ...
๐ŸŒ
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 17, 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?

Find elsewhere
๐ŸŒ
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 ...
๐ŸŒ
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!
Yes, you can use the round() function in Python to round a float to a specific number of decimal places. In your case, you can use round(number, 2) to round the number to 2 decimal places.
๐ŸŒ
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 - In this case, itโ€™s 2, meaning you want to display two decimal places. The f indicates that the value being inserted is a floating-point number. 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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ decimal.html
decimal โ€” Decimal fixed-point and floating-point arithmetic
What methods should be used? A: The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation: >>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01')
๐ŸŒ
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 ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
Add a placeholder where you want to display the price: price = 49 txt = "The price is {} dollars" print(txt.format(price)) Try it Yourself ยป ยท You can add parameters inside the curly brackets to specify how to convert the value: Format the price to be displayed as a number with two decimals: txt = "The price is {:.2f} dollars" Try it Yourself ยป
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to format a number to 2 decimal places in python?
How to Format a Number to 2 Decimal Places in Python? - AskPython
February 27, 2023 - It is pretty simple by using %f formatter or str.format() with โ€œ{:.2f}โ€ as a string and float as a number. Letโ€™s take a look at the first method. The %f formatter is used to input float values or numbers with values after the decimal place.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2f-in-python-what-does-it-mean
%.2f in Python โ€“ What does it Mean?
June 22, 2022 - The resulting value in our example is 2.0 which was returned when 1.9876 was rounded up to one decimal place. Let's use %.2f and see what happens. floatNumber = 1.9876 print("%.2f" % floatNumber) # 1.99 ยท As expected, the floating point number (1.9876) was rounded up to two decimal places โ€“ 1.99.
๐ŸŒ
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 - You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. Youโ€™ll see more of these later.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
An arg_name is treated as a number if a call to str.isdecimal() on the string would return true. If the numerical arg_names in a format string are 0, 1, 2, โ€ฆ in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, โ€ฆ will be automatically inserted in that order.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
In this case, the float x is rounded to two decimal points, resulting in the value 3.14.Alternatively, you can use the format function to format a float as a string with a fixed number of decimal points. Here is an example of how to use format to limit a float to two decimal points: x = 3.14159265 ...
๐ŸŒ
TestMu AI Community
community.testmuai.com โ€บ ask a question
How can I format a decimal to always show 2 decimal places in Python? - TestMu AI Community
December 2, 2024 - How can I format a decimal to always show 2 decimal places in Python? I want to display values like: - 49 as 49.00 - 54.9 as 54.90 Regardless of the number of decimal places, I want to ensure that the decimal is always displayed with 2 decimal places. This is for displaying monetary values, ...