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

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
python - How to display a float with two decimal places? - Stack Overflow
What you're doing with manual fixed ... by the decimal module (which can be set to arbitrary levels of base-10 precision). 2022-09-22T03:12:49.307Z+00:00 ... %-formatting is not new to Python 3, and is already covered by this answer from 6 years earlier 2020-06-14T17:57:36.713Z+00:00 ... If you want to get a floating point value ... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I format a decimal to always show 2 decimal places? - Stack Overflow
It doesn't have any effect in this case with a float conversion, but it prevents an unexpected type error when converting to strings. Consider r = 1; "%s" % r; r = (1, 2); "%s" % r versus r = 1; "%s" % (r,); r = (1,2 ); "%s" % (r,). As such, most sophisticated coding styles in python use the unconditional tuple 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... More on stackoverflow.com
🌐 stackoverflow.com
How to eliminate trailing zeros?
Since you're already formatting the number as a string, you could just check to see if the last value is a '0', then remove it. Not an efficient solution, but something readable so you have an idea: number = 25.159 num_string = format(number, ".2f") if num_string[-1] == '0': formatted_num = num_string[:-1] else: formatted_num = num_string print(formatted_num) More on reddit.com
🌐 r/learnpython
13
3
October 3, 2019
🌐
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) ......
🌐
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.
🌐
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....
🌐
PythonHow
pythonhow.com › how › limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
Here is an example of how to use ... decimal points y = "{:.2f}".format(x) print(y) # Output: "3.14"The format function takes a format string as the first argument and the value to format as the second argument....
🌐
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 - In this article, we learn how to format a number to 2-decimal places by using two ways. You can show the result using %f formatted, written inside quotation marks, and the % modulo operator separates it from the float number “%f” % num.
Find elsewhere
🌐
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 ...
🌐
DataCamp
datacamp.com › tutorial › python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - The round() function is Python’s built-in function for rounding float point numbers to the specified number of decimal places. You can specify the number of decimal places to round by providing a value in the second argument. The example below prints 34.15.
🌐
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
🌐
Python Guides
pythonguides.com › python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - The %.2f acts as a placeholder that Python fills with your variable, truncated to two decimal places. If you are a data scientist in the USA working with large datasets, you likely use Pandas. Formatting an entire column to 2 decimal places is a common task. I frequently use the pd.options...
🌐
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 use Python’s format specifiers ... has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field....
🌐
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 ... 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 lab...
🌐
TutorialsPoint
tutorialspoint.com › How-to-display-a-float-with-two-decimal-places-in-Python
How to round down to 2 decimals a float using Python?
December 7, 2022 - # input floating-point number inputNumber = 3.367824 # rounding the number up to 2 decimal places roundedNumber = round(inputNumber, 2) # print the rounded value of floating-point number up to 2 decimals print("Rounding 3.367824 upto 2 decimal ...
🌐
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.
🌐
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.
🌐
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 use the Python round() function, you simply need to pass the float as the first argument and the value 2 as the second argument.
🌐
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 decimal module provides various functions that can be used to get two decimal places of a float value. The Decimal() function is used to convert the float-point value to a decimal object. Then the quantize() function is used to set the decimal ...