I had the same question.

Looking at the Python documentation it seems that g also supports precision values:

General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

I don't know, why the other answers don't use this and I am not very experienced in Python, but it works.

This can be simply achieved by using format(0.00001, '.10g') where 10 is the precision you want.

Answer from clel on Stack Overflow
🌐
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 - In its most basic form, this allows you to define the precision, or number of decimal places, the float will be displayed with. 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' To use Python’s format specifiers in a replacement field, you separate them from the expression with a colon (:).
🌐
Wikipedia
en.wikipedia.org › wiki › IEEE_754
IEEE 754 - Wikipedia
1 month ago - The standard defines five basic formats that are named for their numeric base and the number of bits used in their interchange encoding. There are three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits).
Discussions

python - Limiting floats to two decimal points - Stack Overflow
This is not a bug: it’s a result ... as a float. 2019-05-18T22:14:08.407Z+00:00 ... Note that if you try to use this method to print out a number such as 1.00000 it will only print out 1.0, regardless of how many decimal points you specify. 2019-08-03T16:36:04.177Z+00:00 ... Let me give an example in Python 3.6's f-string/template-string format, which I think ... More on stackoverflow.com
🌐 stackoverflow.com
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
string - How to make Python format floats with certain amount of significant digits? - Stack Overflow
I want my Python (2.4.3) output numbers to have a certain format. Specifically, if the number is a terminating decimal with 6 significant More on stackoverflow.com
🌐 stackoverflow.com
Trying to format a list of floats with f-strings
You can't format an entire list all at once. You have to format each number individually, and then unpack that to print it: print("Roots:", *(f"{x:.3f}" for x in roots)) or you could use join to combine the formatted numbers: print("Roots:", ' '.join(f"{x:.3f}" for x in roots)) More on reddit.com
🌐 r/learnpython
6
2
July 23, 2018
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
🌐
Medium
medium.com › @coucoucamille › float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Simple Float Formatting in Python Python’s built-in format() function allows you to format float in any way you prefer. 1. Round Float to 2 Decimal Places Syntax: {:.2f}.format(num) for rounding to …
🌐
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 ...
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_strings_format.asp
Python - Format Strings
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... age = 36 #This will produce an error: txt = "My name is John, I am " + age print(txt) Try it Yourself » · But we can combine strings and numbers by using f-strings or the format() method!
🌐
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 - However, modern formatting methods ... format a floating-point number with two decimal places in Python, you can use the .2f format specifier....
🌐
Python documentation
docs.python.org › 3 › tutorial › floatingpoint.html
15. Floating-Point Arithmetic: Issues and Limitations — Python 3.14.3 documentation
The float.hex() method expresses a float in hexadecimal (base 16), again giving the exact value stored by your computer: ... Since the representation is exact, it is useful for reliably porting values across different versions of Python (platform independence) and exchanging data with other languages that support the same format (such as Java and C99).
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
2 weeks ago - This syntax is similar to the syntax specified in section 6.4.4.2 of the C99 standard, and also to the syntax used in Java 1.5 onwards. In particular, the output of float.hex() is usable as a hexadecimal floating-point literal in C or Java code, and hexadecimal strings produced by C’s %a format ...
🌐
Mooc
programming-25.mooc.fi › part-4 › 5-print-statement-formatting
Print statement formatting - Python Programming MOOC 2025
The format specifier .2f states that we want to display 2 decimals. The letter f at the end means that we want the variable to be displayed as a float, i.e.
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
When doing so, float() is used to convert the integer to a floating-point number before formatting.
🌐
AskPython
askpython.com › home › python floating point formatting: 2 simple methods
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - In this example, the :.2f inside the curly braces states that the floating-point number should be formatted with two decimal places. Running this code would output 123.46. ... Here, :10.2f states that the total width of the formatted string should be 10 characters, including two decimal places. The output, in this case, would be 123.46 · Python’s format method offers a versatile approach to string formatting.
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
Of course it is also possible to format numbers. ... Similar to strings numbers can also be constrained to a specific width. ... Again similar to truncating strings the precision for floating point numbers limits the number of positions after the decimal point.
🌐
Built In
builtin.com › data-science › how-to-use-make-float-in-python
How to Use Float in Python (With Sample Code!) | Built In
Summary: In Python, floats are a common data format that allows users to work with decimal numbers. While they produce more precise calculations than integers, floats can still lead to errors.
🌐
Mimo
mimo.org › glossary › python › float
Python Floats: Coding Essentials | Learn Now
Beware of Precision Issues: Standard floats can have small precision errors due to how computers store them. For high-precision financial calculations, use Python's decimal module instead. Format with F-Strings: Use f-strings with format specifiers (e.g., {my_float:.2f}) to easily control the number of decimal places when displaying floats.
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
Here is a list of all the formatting types. Before Python 3.6 we used the format() method to format strings.
🌐
The Teclado Blog
blog.teclado.com › python-formatting-numbers-for-printing
Formatting Numbers for Printing in Python
March 23, 2023 - For large numbers we often write a separator character (usually a comma, space, or period) to make it easier to read. We can specify this in Python using a comma or underscore character after the colon. x = 1000000 print(f"{x:,}") # 1,000,000 print(f"{x:_}") # 1_000_000 · This also works with floats, and the precision formatting, with the comma coming first: