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

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
rounding - How to round to 2 decimals with Python? - Stack Overflow
Still doesn't work with Python 3.8.2. 2020-03-01T22:42:14.523Z+00:00 ... round(2.675,2) gives 2.67, because 2.675 cannot be represented exactly by computer as the nature of float number. Run from decimal import Decimal; Decimal.from_float(2.675) , you can find 2.675 is actually stored as ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to display a float with two decimal places? - Stack Overflow
Closed 2 years ago. 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). How can I do this in Python? More on stackoverflow.com
🌐 stackoverflow.com
how to limit or round a float to only two decimals without rounding up
You can try the solution here: https://stackoverflow.com/a/62435913 More on reddit.com
🌐 r/learnpython
12
5
March 8, 2024
🌐
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!
Here's an example: number = 10.1234567 rounded_number = round(number, 2) print(rounded_number) # Output: 10.12 Alternatively, you can use the format() function to format the number as a string with 2 decimal places: number = 10.1234567 ...
🌐
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 - In Python, we can use the decimal module to round floating-point numbers to a specific number of decimal places with higher precision and control compared to the built-in floating-point arithmetic.
🌐
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....
🌐
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.
Find elsewhere
🌐
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 - The built-in Python function round() can round a floating-point number to an integer or to a specified number of decimal places. You can specify the number of decimal places you want and the number you want to round using two different ...
🌐
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.
🌐
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.
🌐
w3tutorials
w3tutorials.net › blog › round-float-to-2-digits-after-dot-in-python
How to Round a Float to 2 Decimal Places in Python: A Pythonic, Less Verbose Method (Avoiding round() Pitfalls) — w3tutorials.net
You understand and accept its floating-point and bankers rounding behavior. For Financial/High-Precision Use Cases: Use Python’s decimal.Decimal module for exact decimal arithmetic. Example: from decimal import Decimal, getcontext getcontext().rounding = ROUND_HALF_UP # Use standard rounding (not bankers) precise_value = Decimal("2.675").quantize(Decimal("0.01")) print(precise_value) # Output: Decimal('2.68') (exact)
🌐
Sentry
sentry.io › sentry answers › python › limit floats to two decimal points in python
Limit floats to two decimal points in Python | Sentry
March 15, 2023 - We can do this using Python’s round() function, which rounds a given number to a specified number of decimal places. For example: my_number = 9.489999999999 rounded = round(my_number, 2) print(rounded) # will output "9.49"
🌐
Python Guides
pythonguides.com › python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - 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.
🌐
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 - Create a variable to store the input floating-point number. Use the format() function (It gives back a formatted version of the input value that has been specified by the format specifier) to round the number upto the 2 decimal places by passing ...
🌐
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).
🌐
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 ...
🌐
Python Shiksha
python.shiksha › home › tips › 3 python ways to limit float up to two decimal places
3 Python ways to Limit float up to two decimal places - Python Shiksha
July 18, 2021 - But we can also use this % operator in Python print statements in order to restrict our floats up to two decimal places. b= 25.7587852 c=19.568231469 print("%.2f"%b) print("%.2f"%c)
🌐
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 - We can use str.format() function to display float value with two decimal places. It keeps the actual value intact and is simple to implement. ... Sir, your shopping bill amount is Rs.