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
2333

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
🌐
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 format to limit a float to two decimal points: x = 3.14159265 # Format x as a string with two 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.
Discussions

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
How to print float value with 3 decimal precision?
You can use format specifiers to do this: format(1/3, ".3f"). See https://pyformat.info/#number_padding . Note that I'm using the format() function rather than .format(), but in your case because you are just concatenating them together, you might want "The hypotenuse of a right triangle with sides {a:.3f} and {b:.3f} has length {c:.3f}".format(a=a, b=b, c=c) Alternatively, you can use Python 3.6's f-strings. "{a}".format(a=a) can be replaced with f"{a}". More on reddit.com
🌐 r/learnpython
3
3
September 10, 2017
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
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
You generally don't round until you go to display the value. You can use round for that, but since you just want to display it with two digits, you can use string formatting . In your case, something like print(f'{totalPay:.2f}'). .2f is the format specifier, saying round the float (f) to two decimal places (.2). More on reddit.com
🌐 r/learnpython
9
7
January 28, 2020
🌐
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) ... format() is a very useful method while programming in python ...
🌐
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 - How to limit floats to two decimal points in Python? One solution to limit floats to two decimal points is to use the round() function, which rounds a
🌐
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" Due to the limitations ...
🌐
Tutorialdeep
tutorialdeep.com › knowhow › python faqs › how to limit float to two decimal places in python
How to Limit Float To Two Decimal Places in Python -
May 18, 2021 - learn how to limit float to two decimal places in Python. The short answer is: use the Python format() or round() to convert float to two
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › how to limit floats to two decimal points in python?
How to Limit Floats to Two Decimal Points in Python? - Be on the Right Side of Change
May 31, 2022 - Recommended Tutorials to understand how string formatting works in Python: ... ✒️ Solution: You have to use the format() method with .2f placed inside the placeholder that will help to return the value up to two decimal places.
🌐
W3docs
w3docs.com › python
Limiting floats to two decimal points
To limit a float to two decimal points, you can use the round() function.
🌐
YouTube
youtube.com › finxter
How to Limit Floats to Two Decimal Points in Python? - YouTube
Full Tutorial: https://blog.finxter.com/limit-floats-to-two-decimal-points-python/Python round() — A Simple Guide with Video : https://blog.finxter.com/pytho
Published: February 11, 2022
Views: 7K
🌐
pythontutorials
pythontutorials.net › blog › limiting-floats-to-two-decimal-points
How to Limit Floats to Two Decimal Points in Python: Fixing the round() Function Issue to Get 13.95 — pythontutorials.net
This blog will demystify why round() sometimes fails, explore alternative methods to limit floats to two decimal places, and provide a step-by-step solution to reliably get 13.95 when you need it. ... At first glance, Python’s built-in round() function seems like the obvious solution: round(number, decimals) rounds number to decimals places.
🌐
JanBask Training
janbasktraining.com › community › python-python › limiting-floats-to-two-decimal-points
Limiting floats to two decimal points | JanBask Training Community
August 18, 2025 - How can you limit floats to two decimal points, and what methods are available to achieve this? This is a common requirement in programming when formatting numerical outp
🌐
Devjugal
til.devjugal.com › python › limit-floating-point-to-two-decimal-places
Limit Floating Point To Two Decimal Places | Today I Learned
We can limit floating points to two decimals in Python as - Copy · >>> "{:.2f}".format(3.145678912345) '3.15' Source: StackOverFlow · PreviousGet IP Address From Hostname · NextMeasure Latency Using TCP ·
🌐
GeeksforGeeks
geeksforgeeks.org › precision-handling-python
Precision Handling in Python - GeeksforGeeks
August 9, 2024 - Using round(x,n) - This function takes 2 arguments, number, and the number till which we want the decimal part rounded. Using f-string - PEP 498 introduced a new string formatting mechanism known as Literal String Interpolation or more commonly ...
🌐
Nexus Pie
nexuspie.com › home › p&c
How To Limit Float To 2 Decimal Places In Python? | 2 Easy Ways
May 28, 2024 - Simply adjust the second argument in either method. For 3 decimal places, use round(float_value, 3) or .3f in string formatting. The round() function handles rounding according to standard rounding rules (halfway up goes up). While this article focuses on Python, C offers similar functionalities.
🌐
Unanswered
unanswered.io › home › technology › how to limit a float to 2 decimal places in python
Limit Float to 2 Decimal Places in Python
August 23, 2026 - The round function limits a float to two decimal places with round(number, 2). String formatting provides another clean option for output. Choose based on whether you need the value for math or just for showing.[2] Why do Python floats show ...
🌐
Stack Abuse
stackabuse.com › bytes › limiting-float-decimal-points-in-python
Limiting Float Decimal Points in Python
August 31, 2023 - We'll cover some of the most common methods here: The round() function is a built-in Python function that rounds a number to a specified number of decimal places. By default, it rounds to the nearest whole number, but you can pass a second argument to specify the number of decimal places.
🌐
Medium
medium.com › @scriptopia › python-floats-limit-float-to-two-decimal-places-b875aba400a8
Python Floats — Limit float to two decimal places - Scriptopia - Medium
December 11, 2022 - Python Floats — Limit float to two decimal places Here we are creating a script in Python that will limit the number of decimal places within an existing float. To do this we are using Pythons …
🌐
Quora
quora.com › How-do-you-limit-decimal-places-in-Python
How to limit decimal places in Python - Quora
Answer: Do you mean you want to limit the decimal places when converting a float (or Decimal) to a string format? In that case, string formatting can help you out: [code]In [6]: x = 0.406 In [7]: f'{x:.2f}' Out[7]: '0.41' [/code]The [code ].2[/code] in the format specifier indicates we want 2 ...