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
Since at least 2000, almost all machines use IEEE 754 binary floating-point arithmetic, and almost all platforms map Python floats to IEEE 754 binary64 “double precision” values.
🌐
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?

Discussions

which data type should I use for most accurate calculations? float decimal or python?
Floats are great but unreliable - they have small imprecisions that can quickly add up. More on reddit.com
🌐 r/learnpython
7
1
March 25, 2023
How to deal with float precision?
You have to decide what are you trying to do with the float. In scientific computing, there's tons of research dedicated to changing all sorts of algorithms in ways that make them tolerant to float precision. Universally, you probably shouldn't compare floats as a == b but rather as abs(a - b) < something_small. There are various functions that do stuff like that for you, too, like numpy.isclose(): >>> 0.3 == 3 * 0.1 False >>> abs(0.3 - 3 * 0.1) < 1e-10 True >>> from numpy import isclose >>> isclose(0.3, 3 * 0.1) True It's best to cast the float as a decimal For financial math, it's probably not the worst idea. round before/after calculation Don't round without a good reason to round, especially before or during a calculation. You should have a good idea of the consequences of rounding as they relate to further calculations if you're going to do something like that. You can round when printing without worry, I guess: >>> from math import pi >>> f'{pi:.2f}' '3.14' or you can use some formatter that'll deal with some of the weirdness for you: >>> f'{3*0.1}' '0.30000000000000004' >>> f'{3*0.1:g}' '0.3' I wouldn't just round for the sake of rounding, though. More on reddit.com
🌐 r/learnpython
25
3
June 4, 2020
General way to print floats without the .0 part
I’m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that ... More on discuss.python.org
🌐 discuss.python.org
0
May 19, 2024
python - Is floating point arbitrary precision available? - Stack Overflow
Just for fun and because it was really easy, I've written a short program to generate Grafting numbers, but because of floating point precision issues it's not finding some of the larger examples. ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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....
🌐
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 - For example, the decimal number `0.1` cannot be precisely stored as a binary floating-point number. Instead, it is approximated by a fraction that is as close as possible within the limits of the system’s precision. To see this in action, consider the following Python code:
🌐
AskPython
askpython.com › home › double precision floating values in python
Double precision floating values in Python - AskPython
April 10, 2025 - Python’s built-in float data type provides up to 15 digits of decimal precision. While sufficient for most applications, some numerical computations require even higher precision.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - Given a number, the task is to control its precision either by rounding it or formatting it to a specific number of decimal places. For Example: Input: x = 2.4 Output: Integral value = 2 Smallest integer greater than x = 3 Greatest integer smaller ...
🌐
Mimo
mimo.org › glossary › python › float
Python Floats: Coding Essentials | Learn Now
... Dividing two numbers results ... third = 1 / 3 # Results in a float, approximately 0.3333 · The return value of such operations is always a float, ensuring precision even when the inputs are whole 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 celi() function belongs to python’s math module, therefore to use it we need to first import the math module into our program. ... Returns: The ceil() function returns the smallest integer greater than the floating point number. Example: ...
🌐
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

🌐
Python.org
discuss.python.org › python help
General way to print floats without the .0 part - Python Help - Discussions on Python.org
May 19, 2024 - I’m building SVG code using data interpolation (f-strings and .format), and I have elements (the size of the graph for one) that are internally floats but which are usually integers. But when printing floats, the .0 part is always included. Is there a standard str-interpolation idiom that turns 24.125 into “24.125” but 25.0 into “25” ?
🌐
Python
docs.python.org › 3 › c-api › float.html
Floating-Point Objects — Python 3.14.3 documentation
The pack and unpack functions provide an efficient platform-independent way to store floating-point values as byte strings. The Pack routines produce a bytes string from a C double, and the Unpack routines produce a C double from such a bytes string. The suffix (2, 4 or 8) specifies the number of bytes in the bytes string. On platforms that appear to use IEEE 754 formats these functions work by copying bits. On other platforms, the 2-byte format is identical to the IEEE 754 binary16 half-precision format, the 4-byte format (32-bit) is identical to the IEEE 754 binary32 single precision format, and the 8-byte format to the IEEE 754 binary64 double precision format, although the packing of INFs and NaNs (if such things exist on the platform) isn’t handled correctly, and attempting to unpack a bytes string containing an IEEE INF or NaN will raise an exception.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
2 weeks ago - If the argument is an integer or a floating-point number, a floating-point number with the same value (within Python’s floating-point precision) is returned.
🌐
Python
docs.python.org › 3 › library › decimal.html
decimal — Decimal fixed-point and floating-point arithmetic
For example, Decimal(float('1.1')) converts to Decimal('1.100000000000000088817841970012523233890533447265625'). The context precision does not affect how many digits are stored. That is determined exclusively by the number of digits in value.
🌐
Python⇒Speed
pythonspeed.com › articles › float64-float32-precision
The problem with float32: you only get 16 million values
February 1, 2023 - A floating point number with a given number of bits has three parts: One bit determines whether it’s positive or negative. Most of the bits (the “significand” or “mantissa”) allow to express a range of values at a specific precision level.
Top answer
1 of 5
72

In the standard library, the decimal module may be what you're looking for. Also, I have found mpmath to be quite helpful. The documentation has many great examples as well (unfortunately my office computer does not have mpmath installed; otherwise I would verify a few examples and post them).

One caveat about the decimal module, though. The module contains several in-built functions for simple mathematical operations (e.g. sqrt), but the results from these functions may not always match the corresponding function in math or other modules at higher precisions (although they may be more accurate). For example,

from decimal import *
import math

getcontext().prec = 30
num = Decimal(1) / Decimal(7)

print("   math.sqrt: {0}".format(Decimal(math.sqrt(num))))
print("decimal.sqrt: {0}".format(num.sqrt()))

In Python 3.2.3, this outputs the first two lines

   math.sqrt: 0.37796447300922719758631274089566431939601898193359375
decimal.sqrt: 0.377964473009227227214516536234
actual value: 0.3779644730092272272145165362341800608157513118689214

which as stated, isn't exactly what you would expect, and you can see that the higher the precision, the less the results match. Note that the decimal module does have more accuracy in this example, since it more closely matches the actual value.

2 of 5
11

For this particular problem, decimal is a great way to go, because it stores the decimal digits as tuples!

>>> a = decimal.Decimal(9999999998)
>>> a.as_tuple()
DecimalTuple(sign=0, digits=(9, 9, 9, 9, 9, 9, 9, 9, 9, 8), exponent=0)

Since you're looking for a property that is most naturally expressed in decimal notation, it's a bit silly to use a binary representation. The wikipedia page you linked to didn't indicate how many "non-grafting digits" may appear before the "grafting digits" begin, so this lets you specify:

>>> def isGrafting(dec, max_offset=5):
...     dec_digits = dec.as_tuple().digits
...     sqrt_digits = dec.sqrt().as_tuple().digits
...     windows = [sqrt_digits[o:o + len(dec_digits)] for o in range(max_offset)]
...     return dec_digits in windows
... 
>>> isGrafting(decimal.Decimal(9999999998))
True
>>> isGrafting(decimal.Decimal(77))
True

I think there's a good chance the result of Decimal.sqrt() will be more accurate, at least for this, than the result of math.sqrt() because of the conversion between binary representation and decimal representation. Consider the following, for example:

>>> num = decimal.Decimal(1) / decimal.Decimal(7)
>>> decimal.Decimal(math.sqrt(num) ** 2) * 7
Decimal('0.9999999999999997501998194593')
>>> decimal.Decimal(num.sqrt() ** 2) * 7
Decimal('1.000000000000000000000000000')
🌐
Simplilearn
simplilearn.com › home › resources › software development › python float() function: key concepts [with examples]
Python float() Function: Key Concepts [With Examples]
November 27, 2024 - Learn how the float() function in Python converts a specified value into a floating point number. Syntax: float(value). A key function for numeric conversions.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
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
🌐
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.