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
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
Discussions

python - How to display a float with two decimal places? - Stack Overflow
I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5 โ†’ 5.00, 5.5 โ†’ 5.50, etc)... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Floating point numbers to two decimal places - possible with standard lib?
Iโ€™m trying to get 2 decimal places of 0s (e.g, 9.00) in the budget app project. I tried using string formatting, but the program expects a floating point number, not a string. Casting it to a float results in a single .0 because thatโ€™s how floating point numbers work in Python - that gets ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
0
0
August 24, 2022
floating point - Python, print all floats to 2 decimal places in output - Stack Overflow
I need to output 4 different floats to two decimal places. This is what I have: print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l' Which is very unclean, and ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
how do I limit the amount of decimal values stored in a float variable?
First, if you are not displaying it anywhere, do not do anything. There is no need to unnecessarily reduce accuracy. You are not hurting anything by having extra decimal places. But if you are and want to format it, well, that's just formatting like this https://appdividend.com/2022/06/23/how-to-format-float-values-in-python/ More on reddit.com
๐ŸŒ r/learnpython
6
1
November 20, 2022
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - # 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 ยท I executed the above example code and added the screenshot below. Inside the curly braces, 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.
๐ŸŒ
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 - Like before when we only used %f for the full float value to be showcased, but here we are adding โ€œ%.1fโ€ for the 1 digit to appear after the decimal and โ€œ%.2fโ€ for the 2 digits to appear after the decimal.
๐ŸŒ
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 ... To format a floating-point number with two decimal places in Python, you can use the .2f format specifier....
๐ŸŒ
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 it the value 3.1415926 (a floating-point number). x = 3.1415926 # Define a variable 'y' and assign it the value 12.9999 (a floating-point number).
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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!
to get it done, we can use an f-string like this: val = 42.1 print(f'{val:.2f}') # for more details have a look at the python docs result is: 42.10 An answer proposed by Lothar
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ python
Floating point numbers to two decimal places - possible with standard lib? - Python - The freeCodeCamp Forum
August 24, 2022 - Iโ€™m trying to get 2 decimal places of 0s (e.g, 9.00) in the budget app project. I tried using string formatting, but the program expects a floating point number, not a string. Casting it to a float results in a single .0 because thatโ€™s how floating point numbers work in Python - that gets ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-display-a-float-with-two-decimal-places-in-Python
How to display a float with two decimal places in Python?
December 7, 2022 - Pass input number, 2 (decimal value) as arguments to the round() function to round the input number upto the 2 decimal places. Print the rounded value of the input floating-point number upto 2 decimals.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-print-float-with-2-decimals
Python Print Float with 2 Decimals - CodeRivers
April 14, 2025 - To round a floating-point number to 2 decimal places, you can use it as follows: float_number = 3.14159 rounded_number = round(float_number, 2) print(rounded_number) In this example, the round() function takes two arguments: the number to be rounded (float_number) and the number of digits to ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2f-in-python-what-does-it-mean
%.2f in Python โ€“ What does it Mean?
June 22, 2022 - You can play around with the code to see what happens as you change the number in the formatter. Another formatting method we can use with floating point numbers in Python is the %d formatter.
๐ŸŒ
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)...
๐ŸŒ
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 - # Quick examples of limiting floats to two decimal points # Example 1: Using the round() function rounded_num = round(num, 2) print(rounded_num) # Example 2: Using string formatting rounded_num = "{:.2f}".format(num) print(rounded_num) # Example ...
๐ŸŒ
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 ...
๐ŸŒ
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 Python, %.2f formats a float to 2 decimal places. Learn what %.2f means, how to use .2f in print statements, f-strings, and format(), with simple examples.
๐ŸŒ
Quora
quora.com โ€บ In-Python-how-could-I-convert-a-float-with-one-decimal-place-into-a-float-with-two-decimal-places-for-example-24-3-to-24-30
In Python, how could I convert a float with one decimal place into a float with two decimal places (for example, 24.3 to 24.30)? - Quora
Answer (1 of 4): You don't convert floats like that. You really mean how to PRINT numbers in different formats. In current Python: x = 24.3 (in the computer, x has at least 10 decimal places) print(f"[x:4.2f]โ€) !!!!! This keyboard does not have the braces key โ€ฆ. use braces instead of [ ] !!!...
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - Studytonight
January 21, 2021 - #import decimal from decimal import getcontext, Decimal # Set the precision getcontext().prec = 3 # Execute 1/7, however cast both numbers as decimals result = Decimal(16.0)/Decimal(7) # Your output will return w/ 6 decimal places print(result) ... This precision takes the total number of digits to be printed. If prec = 2, then the output would be 2.3. The behavior of round() for floats can be surprising.