The comments state the objective is to print to 2 decimal places.

There's a simple answer for Python 3:

>>> num=3.65
>>> "The number is {:.2f}".format(num)
'The number is 3.65'

or equivalently with f-strings (Python 3.6+):

>>> num = 3.65
>>> f"The number is {num:.2f}"
'The number is 3.65'

As always, the float value is an approximation:

>>> "{}".format(num)
'3.65'
>>> "{:.10f}".format(num)
'3.6500000000'
>>> "{:.20f}".format(num)
'3.64999999999999991118'

I think most use cases will want to work with floats and then only print to a specific precision.

Those that want the numbers themselves to be stored to exactly 2 decimal digits of precision, I suggest use the decimal type. More reading on floating point precision for those that are interested.

Answer from Andrew E on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations โ€” Python 3.14.3 documentation
For use cases which require exact ... and high-precision applications. Another form of exact arithmetic is supported by the fractions module which implements arithmetic based on rational numbers (so the numbers like 1/3 can be represented exactly). If you are a heavy user of floating-point operations you should take a look at the NumPy package and many other packages for mathematical and statistical operations supplied by the SciPy project. See <https://scipy.org>. Python provides tools that may help ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-round-float-to-3-decimals-in-python
5 Best Ways to Round Float to 3 Decimals in Python โ€“ Be on the Right Side of Change
๐Ÿ’ก Problem Formulation: When working with floating-point numbers in Python, itโ€™s often necessary to round them to a specific number of decimal places for display or other calculation purposes. For instance, when dealing with monetary values or precise measurements, you may need to round a float like 3.14159265 to 3.142.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ decimal.html
decimal โ€” Decimal fixed-point and floating-point arithmetic โ€” Python 3.14.3 documentation
For example, Decimal((0, (1, 4, 1, 4), -3)) returns Decimal('1.414'). If value is a float, the binary floating-point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - ... from decimal import Decimal, getcontext getcontext().prec = 3 r = Decimal('3') / Decimal('9') print(r) ... Decimal() ensures accurate decimal arithmetic without floating-point inaccuracies.
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
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python float precision to 3
Python float precision to 3
July 10, 2023 - In Python, the float type does not inherently have a fixed precision. The precision of a float is determined by the floating-point representation used by the underlying hardware, which is typically based on the IEEE 754 standard.
๐ŸŒ
Medium
medium.com โ€บ @goldengrisha โ€บ understanding-floating-point-precision-issues-in-python-a-practical-guide-5e17b2f14057
Understanding Floating-Point Precision Issues in Python: A Practical Guide | by Gregory Kovalchuk | Medium
September 25, 2024 - Here, adding `0.1` and `0.2` should yield `0.3`, but instead, we get `0.30000000000000004`. The reason is that neither `0.1` nor `0.2` has an exact binary representation, and the small rounding errors accumulate when the values are added. Letโ€™s consider a more complex scenario, one involving a physics simulation where we calculate forces. Suppose we have the following Python function to compute a force value `v(k, n)` and sum it over multiple iterations: def v(k: int, n: int) -> float: return 1 / (k * (n + 1) ** (2 * k)) def doubles(maxk: int, maxn: int) -> float: total = 0 old_total = 0 for k in range(1, maxk + 1): local_total = 0 for n in range(1, maxn + 1): local_total += v(k, n) old_total += v(k, n) # Updates old_total within the loop total += local_total return total
Find elsewhere
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ decimal
Python Decimal - high-precision calculations in Python with Decimal
Neither of the types is perfect; generally, decimal types are better suited for financial and monetary calculations, while the double/float types for scientific calculations. The Decimal has a default precision of 28 places, while the float has 18 places. ... #!/usr/bin/python from decimal ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [python] adding floating point number causes precision issues for some numbers but not othera.
r/learnprogramming on Reddit: [Python] Adding floating point number causes precision issues for some numbers but not othera.
January 9, 2024 -

I'm not sure if this is a problem with my IDE or if its a Python issue but when I add a floating point number, take 3.14 for example, to some numbers I get a lot of additional 0s.

y = 3.14
y1 = 3.14 + 1
print(3.14)

Output:
4.140000000000001

I noticed this issue occurs when I add 1 to 4 but if I add 5 I get 8.14 instead, without all of the 0s.

Why does this only happen when adding certain numbers?

๐ŸŒ
Shiksha
shiksha.com โ€บ home โ€บ it & software โ€บ it & software articles โ€บ programming articles โ€บ precision handling in python
Precision Handling in Python - Shiksha Online
October 31, 2022 - The format() function in python can be used to format a float value for precision based on the format specifier. ... format_specifier: The format specifier in the above syntax specifies how the value is to be formatted.
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter09.02-Floating-Point-Numbers.html
Floating Point Numbers โ€” Python Numerical Methods
Instead of utilizing each bit as ... \(f\), which is the coefficient of the exponent. Almost all platforms map Python floats to the IEEE754 double precision - 64 total bits....
๐ŸŒ
Luc
anh.cs.luc.edu โ€บ handsonPythonTutorial โ€บ float.html
1.14. Decimals, Floats, and Floating Point Arithmetic โ€” Hands-on Python Tutorial for Python 3
Try each in the Shell (and guess the resulting type): [1] ... Exponentiation is finding powers. In mathematical notation, (3)(3)(3)(3)=34. In Python there is no fancy typography with raised exponent symbols like the 4, so Python uses ** before a power: Try in the Shell:
๐ŸŒ
DEV Community
dev.to โ€บ mike-vincent โ€บ quarks-outlines-python-floating-point-numbers-3jpb
Quarkโ€™s Outlines: Python Floating Point Numbers - DEV Community
August 9, 2025 - You can also write a floating point number using scientific notation, which uses e or E to show powers of ten. For example, 1e3 means 1000.0. Python does not support single-precision floats.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-37088.html
precision in formatting float
Quote:The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ scientific precision in python?
r/learnpython on Reddit: Scientific Precision in Python?
March 1, 2021 -

When the matter is precision - like, really precise - floats are crap. Even double precision can be a gamble when working with scientific data - very small and very large numbers.

What are the best options when I need to work with numbers on the -15th and +20th orders of magnitude? (at the same time, mind you)

Is the decimal.py module precise enough for those sorts of calculations? If not, is it possible to get precise results with python or will I have to write some matlab modules to crunch my numbers?

๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-handle-python-float-precision-425669
How to handle Python float precision | LabEx
In Python, floats are implemented using the IEEE 754 double-precision binary floating-point format. This means numbers are stored in binary representation, which can lead to some unexpected behaviors. ## Basic float demonstration x = 0.1 y = 0.2 print(x + y) ## Might not be exactly 0.3
๐ŸŒ
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 - The code below displays the same calculation as before, only itโ€™s displayed more neatly: ... >>> f"One third, rounded to two decimal places is: {1 / 3:.2f}" 'One third, rounded to two decimal places is: 0.33'
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to deal with float precision?
r/learnpython on Reddit: How to deal with float precision?
June 4, 2020 -

I know this is not a Python-specific issue. I read some posts saying it's best to cast the float as a decimal, and others say round before/after calculation. What are your recommendations? Is there a "pythonic" way to do this?

Edit: Decimal

๐ŸŒ
LAAC Technology
laac.dev โ€บ blog โ€บ float-vs-decimal-python
Float vs Decimal in Python | LAAC Technology
March 4, 2021 - >>> from decimal import Decimal >>> Decimal('1') / Decimal('3') * Decimal('3') == Decimal('1') False >>> Decimal('1') / Decimal('3') * Decimal('3') Decimal('0.9999999999999999999999999999') Michael also points out that the float equivalent does not run into precision issues. ... Decimal have their own hidden rounding that cause precision issues, and to eliminate the hidden routing, you need to use Pythonโ€™s fractions module.