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
2332

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
🌐
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 - You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. You’ll see more of these later.
Discussions

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
19
0
May 19, 2024
How can I control the number of decimals when printing a float?
value = 123.456789 for p in range(1, 11): print(f'{value:.{p}f}') This prints: 123.5 123.46 123.457 123.4568 123.45679 123.456789 123.4567890 123.45678900 123.456789000 123.4567890000 More on reddit.com
🌐 r/learnpython
8
4
August 5, 2021
keep trailing 0's in floats?
The Decimal library is great for this. This helps you avoid floating point arithmetic errors too. More on reddit.com
🌐 r/learnpython
6
2
July 20, 2017
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
People also ask

How to format floating numbers using inbuilt methods in Python?
We can use the 'format' method to specify the width and assignment of the floating numbers.
🌐
askpython.com
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I limit a float to only show 2 decimal places?
You can limit a float to 2 decimal places using either f-strings or the format() method. For f-strings, use f"{value:.2f}" and for format(), use "{:.2f}".format(value).
🌐
askpython.com
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
How do I display infinity or nan values properly?
Use the .2g format to display inf, -inf, and nan in a consistent readable format like you would expect in a calculator.
🌐
askpython.com
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
🌐
AskPython
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
April 10, 2025 - To format floats, include the desired precision in curly brackets after the f: ... In this example, the :.2f inside the curly braces states that the floating-point number should be formatted with two decimal places.
🌐
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 par…
🌐
Medium
medium.com › @coucoucamille › float-formatting-in-python-ccb023b86417
Simple Float Formatting in Python | by Coucou Camille | Medium
June 15, 2022 - Syntax: {:.2f}.format(num) for rounding to 2 decimal places. {} marks a replacement field · : introduces a format specifier · .2 specify the precision as 2, or any other number ·
🌐
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 number 2 specifies the precision, which is the number of digits to display after the decimal point. The letter f represents the data type, which is a floating-point number in this case.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - Decimal() ensures accurate decimal arithmetic without floating-point inaccuracies. This method is used when you need a number to match an exact decimal structure. It allows you to enforce a fixed number of decimal digits, making it reliable for values like currency and measurements. ... Explanation: quantize(Decimal('0.00')) formats the number to exactly 2 decimal places. This method lets you control precision at the integer level.
🌐
freeCodeCamp
freecodecamp.org › news › 2f-in-python-what-does-it-mean
%.2f in Python – What does it Mean?
June 22, 2022 - The %f formatter is specifically used for formatting float values (numbers with decimals). We can use the %f formatter to specify the number of decimal numbers to be returned when a fl...
🌐
mkaz.blog
mkaz.blog › working-with-python › string-formatting
Python String Formatting: Complete Guide - mkaz.blog
Format specification syntax: ... (space for positive) width: Minimum field width · ,: Use comma as thousands separator · precision: Digits after decimal point · type: f (float), d (decimal), e (scientific), % (percentage) F-strings support more than just variable ...
🌐
LinkedIn
linkedin.com › pulse › python-strings-format-mr-examples
Python Strings Format
May 18, 2023 - When using the .2f format specifier, the number will be rounded to two decimal places and displayed with two digits after the decimal point. If the number has fewer than two decimal places, it will be padded with zeros.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - GeeksforGeeks
December 31, 2024 - We can use string formatting with % operator to format a float value to two decimal places.Here we are using string formatting with % operator to round the given value up to two decimals.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 8-4-string-formatting
8.4 String formatting - Introduction to Python Programming | OpenStax
March 13, 2024 - In the given syntax, The index field refers to the index of the argument. The width field refers to the minimum length of the string. The precision field refers to the floating-point precision of the given number.
🌐
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
🌐
Cjtu
cjtu.github.io › spirl › python_str-formatting.html
3.10. String Formatting (Interactive) — Scientific Programming<br>In Real Life
Below we use the % operator after the string to include the three elements into the three locations denoted by % within the string. We use three different format codes for the three numbers included in the string: ... The float format code rounding to 2 decimal places .2f.
🌐
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 - Here str.format() refers to a method of using ***"{:.2f}"*** as the str in the above format. The string along with the format() method will restrict the floating-point numbers up to 2 decimal places.
🌐
GitHub
gist.github.com › dalek7 › c2e2289770eb195fb7fe8961173c1098
Python string format : float precision · GitHub
Python string format : float precision. GitHub Gist: instantly share code, notes, and snippets.
🌐
Real Python
realpython.com › python-formatted-output
A Guide to Modern Python String Formatting Tools – Real Python
February 1, 2025 - In practice, 0 is superfluous and was probably included as a convenience for developers who are familiar with the string modulo operator’s similar 0 conversion flag. ... The grouping_option component allows you to include a grouping separator character in numeric outputs. For decimal and floating-point presentation types, grouping_option may be either a comma (,) or an underscore (_).
🌐
CopyProgramming
copyprogramming.com › howto › python-python-format-float-precision-in-fstring
Python F-String Float Precision Formatting: Complete Guide for 2026 - Python f string float precision formatting complete guide
November 28, 2025 - F-strings are the recommended method for float formatting in Python 3.6+ and 2026 best practices, offering superior readability, speed, and maintainability compared to .format() and % formatting · Precision specification uses the .Nf syntax where N is the number of decimal places; always include ...