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.
🌐
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 - y = 12.9999 # Print an empty 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 label. print("Formatted Number: ...
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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.
🌐
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 ...
🌐
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.
🌐
YouTube
youtube.com › watch
Python Programming – Print A Float with Two Decimal Places - YouTube
Hi, in this video we will solve the following exercise: “Write a python program that receives one float number from the user and print the number with two de...
Published   April 17, 2022
🌐
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)...
🌐
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 ...
🌐
Medium
medium.com › @coucoucamille › float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Python’s built-in format() function allows you to format float in any way you prefer. Syntax: {:.2f}.format(num) for rounding to 2 decimal places. ... Syntax: "{:+.2f}".format(num) for positive sign + ; and "{:-.2f}".format(num) for positive ...
🌐
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 ...
🌐
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 ...
🌐
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.
🌐
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 - Use the ceil() function(returns a ceiling value of the number i.e., the smallest integer greater than or equal to the number), to round the number upto the 2 decimal places and print the resultant number.
🌐
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.