You should use the new format specifications to define how your value should be represented:

>>> from math import pi  # pi ~ 3.141592653589793
>>> '{0:.2f}'.format(pi)
'3.14'

The documentation can be a bit obtuse at times, so I recommend the following, easier readable references:

  • the Python String Format Cookbook: shows examples of the new-style .format() string formatting
  • pyformat.info: compares the old-style % string formatting with the new-style .format() string formatting

Python 3.6 introduced literal string interpolation (also known as f-strings) so now you can write the above even more succinct as:

>>> f'{pi:.2f}'
'3.14'
Answer from BioGeek on Stack Overflow
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
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-print-2-decimal-places
How to Print Two Decimal Places in Python
December 22, 2025 - # Monthly subscription cost for ... the :.2f is the magic part. The colon starts the format specifier, the .2 indicates two decimal places, and the f tells Python to treat the value as a float....
๐ŸŒ
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....
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to format a number to 2 decimal places in python?
How to Format a Number to 2 Decimal Places in Python? - AskPython
February 27, 2023 - Here โ€œ{:.2f}โ€ is a string where we have mentioned .2f, which means 2 digits after the decimal in the float number. Then we provide our float value on which the action will take place in the format function. In the result, we are getting the same result using two different ways. You can check The Python ...
๐ŸŒ
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 - To format a float for neat display within a Python f-string, you can use a format specifier. In its most basic form, this allows you to define the precision, or number of decimal places, the float will be displayed with.
๐ŸŒ
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 ... 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....
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ pythonic way to show 2 decimal places in f-strings?
r/learnpython on Reddit: Pythonic way to show 2 decimal places in f-strings?
January 15, 2018 -

I've got a number that needs to be rounded to 2 decimal places. Getting the round is easy, but if the number is a whole number or only has 1 digit beyond the decimal the rounding doesn't properly show 2 decimal places.

answer = 1
print(round(float(answer),2))

>> 1.0

I really like f-strings, so I would use this:

answer = 1
print(f"{round(float(answer),2):.2f}")

>> 1.00

Is there a neater or more readable method of doing this?

๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ string โ€บ python-data-type-string-exercise-30.php
Python: Print the following floating numbers upto 2 decimal places - w3resource
... # Define a variable 'x' and ... line for spacing. print() # Print the original value of 'x' with a label. print("Original Number: ", x) # Format the value of 'x' to two decimal places and print it with a lab...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ 2f-in-python-what-does-it-mean
%.2f in Python โ€“ What does it Mean?
June 22, 2022 - Note that the %f formatter must be nested inside quotation marks, and should be separated from the floating number which it is formatting by a modulo operator (%): "%f" % floatNumber. Let's take a look at another example. floatNumber = 1.9876 print("%.1f" % floatNumber) # 2.0 ยท In the code above, we added .1 between % and f in the %f operator. This means that we want the number to be rounded up to one decimal place.
๐ŸŒ
mkaz.blog
mkaz.blog โ€บ code โ€บ python-string-format-cookbook
String Formatting - mkaz.blog
value = 1234.5678 # Basic decimal places print(f"Two decimals: {value:.2f}") # 1234.57 print(f"No decimals: {value:.0f}") # 1235 print(f"With sign: {value:+.2f}") # +1234.57 # Padding and alignment print(f"Right aligned: {value:10.2f}") # 1234.57 print(f"Left aligned: {value:<10.2f}") # 1234.57 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-round-floating-value-to-two-decimals-in-python
How to Round Floating Value to Two Decimals in Python - GeeksforGeeks
July 23, 2025 - from decimal import Decimal, ... specifies that we want to round to two decimal places n2 = n1.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) print(n2) ......
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-string-to-float-with-2-decimals-easy-conversion-guide
Python String to Float with 2 Decimals: Easy Conversion Guide โ€“ Be on the Right Side of Change
Hereโ€™s the syntax for a float with two decimal places: your_float = 7.326 formatted_float = f"{your_float:.2f}" print(formatted_float) # Output: 7.33 ยท F-strings are a powerful and friendly way to handle precision and formatting directly within your string literals. In this section, youโ€™ll see how to translate string representations of numbers in Python into float objects, specifically focusing on maintaining two decimal places.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ how to display 2 decimal places in python | example code
How to display 2 decimal places in Python | Example code
January 11, 2023 - Simple example code use str.format(number) with โ€œ{:.2f}โ€ as a string and float as a number to return a string representation of the number with two decimal places. fnum = 7.154327 res = "{:.2f}".format(fnum) print(res) Output: Answer: Use ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
July 23, 2025 - The f-string in Python is used to format a string. It can also be used to get 2 decimal places of a float value.
๐ŸŒ
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 - However, there are several other methods for limiting floats to two decimal points in Python, including string formatting, f-strings, and the Decimal module. In this article, we will explain all these methods with examples. These are examples of different methods of limiting floats to two decimal points in Python. These examples should give you a high-level overview of each method and its syntax. # Quick examples of limiting floats to two decimal points # Example 1: Using the round() function rounded_num = round(num, 2) print(rounded_num) # Example 2: Using string formatting rounded_num = "{:.2f}".format(num) print(rounded_num) # Example 3: Using f-strings rounded_num = f"{num:.2f}" print(rounded_num) # Example 4: Using the format() function rounded_num = format(num, ".2f") print(rounded_num)
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ how to format float to 2 decimal places in python?
How to Format Float to 2 Decimal Places in Python? - Java2Blog
April 13, 2021 - Letโ€™s see how we can use these methods to format a floating-point value to two decimal places. format() is a method in Python that formats specific values and inserts them in the placeholder {} of the string.
๐ŸŒ
mkaz.blog
mkaz.blog โ€บ working-with-python โ€บ string-formatting
Python String Formatting: Complete Guide
value = 1234.5678 # Basic decimal places print(f"Two decimals: {value:.2f}") # 1234.57 print(f"No decimals: {value:.0f}") # 1235 print(f"With sign: {value:+.2f}") # +1234.57 # Padding and alignment print(f"Right aligned: {value:10.2f}") # 1234.57 print(f"Left aligned: {value:<10.2f}") # 1234.57 ...