If you have Python 2.6 or newer, use format:

'{0:.3g}'.format(num)

For Python 2.5 or older:

'%.3g'%(num)

Explanation:

{0}tells format to print the first argument -- in this case, num.

Everything after the colon (:) specifies the format_spec.

.3 sets the precision to 3.

g removes insignificant zeros. See http://en.wikipedia.org/wiki/Printf#fprintf

For example:

tests=[(1.00, '1'),
       (1.2, '1.2'),
       (1.23, '1.23'),
       (1.234, '1.23'),
       (1.2345, '1.23')]

for num, answer in tests:
    result = '{0:.3g}'.format(num)
    if result != answer:
        print('Error: {0} --> {1} != {2}'.format(num, result, answer))
        exit()
    else:
        print('{0} --> {1}'.format(num,result))

yields

1.0 --> 1
1.2 --> 1.2
1.23 --> 1.23
1.234 --> 1.23
1.2345 --> 1.23

Using Python 3.6 or newer, you could use f-strings:

In [40]: num = 1.234; f'{num:.3g}'
Out[40]: '1.23'
Answer from unutbu on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ decimal.html
decimal โ€” Decimal fixed-point and floating-point arithmetic
Decimals can be formatted (with format() built-in or f-strings) in fixed-point or scientific notation, using the same formatting syntax (see Format Specification Mini-Language) as builtin float type:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - Explanation: quantize(Decimal('0.00')) formats the number to exactly 2 decimal places.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-format-decimal-output-in-python-421895
How to format decimal output in Python | LabEx
Learn essential techniques for formatting decimal numbers in Python, including precision control, rounding, and display methods for professional data presentation.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Logging โ†’ format decimal places? - Python Help - Discussions on Python.org
November 8, 2023 - Thereโ€™s is sometimes a good reason to pass values the other way, if they are expensive to compute (see here). But if you already have crmTime around I think this is fine ยท Alternatively, the formatting for the logging message is the same as the % style, you can use %.4f there too
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ rounding-to-two-decimal-places-in-pythonn
Rounding to Two Decimal Places in Python | Codecademy
Letโ€™s look at an example of how we can use str.format() to round a number to two decimal places: ... Letโ€™s look at another approach for string formatting. In Python, the % operator, also called modulus operator, We can use this operator ...
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
Find elsewhere
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ functions โ€บ format.html
format โ€” Python Reference (The Right Way) 0.1 documentation
Then if -4 <= exp < p, the number is formatted with presentation type โ€˜fโ€™ and precision p-1-exp. Otherwise, the number is formatted with presentation type โ€˜eโ€™ and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
The function does not have to be a built-in Python method, you can create your own functions and use them: ... def myconverter(x): return x * 0.3048 txt = f"The plane is flying at a {myconverter(30000)} meter altitude" print(txt) Try it Yourself ยป ยท At the beginning of this chapter we explained how to use the .2f modifier to format a number into a fixed point number with 2 decimals.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3288381 โ€บ how-to-round-to-2-decimal-places-in-python
How to round to 2 decimal places in python | Sololearn: Learn to code for FREE!
Yes, you can use the round() function in Python to round a float to a specific number of decimal places. In your case, you can use round(number, 2) to round the number to 2 decimal places.
๐ŸŒ
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 - Also, using str.format() is simple, where you have to write within the curly braces how many places you want after the decimal followed by the variable name in the format function. Hereโ€™s the official Python documentation to help you understand decimal in Python.
๐ŸŒ
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 - By applying the .2f format specifier within the format() method and using the "{:.2f}" format string, the number is formatted to have two decimal places. The resulting formatted_number is then printed, which outputs 3.14 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-get-two-decimal-places-in-python
How to get two decimal places in Python - GeeksforGeeks
July 23, 2025 - Python format() function is an inbuilt function used to format strings. Unlike f-string, it explicitly takes the expression as arguments. ... The % operator can also be used to get two decimal places in Python.
๐ŸŒ
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...
๐ŸŒ
Luc
anh.cs.luc.edu โ€บ python โ€บ hands-on โ€บ 3.1 โ€บ handsonHtml โ€บ float.html
1.14. Decimals, Floats, and Floating Point Arithmetic โ€” Hands-on Python Tutorial for Python 3
More on that in String Formats for Float Precision. It is sometimes important to know the numeric type of the result of a binary operation. Any combination of +, -, and * with operands of type int produces an int. If there is an operation /, or if either operand is of type float, the result is float. 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:
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ understanding python 3โ€™s decimal formatting โ€” :.0f vs. :.1f
Understanding Python 3's Decimal Formatting โ€” :.0f vs. :.1f - AskPython
May 19, 2023 - Enter first number= 5.789 Enter second number= 15.444 The answer before formatting is= 89.405316 The answer after formatting without digits after decimal point is=89 The answer after formatting upto one decimal point is=89.4 ยท Deciphering the Distinction Between :.0f and :.1f in Python
๐ŸŒ
Linux find Examples
queirozf.com โ€บ entries โ€บ python-number-formatting-examples
Python number formatting examples
August 2, 2023 - Example: truncate to 2 decimal places in f-string ยท num = 1.12745 formatted = f"{num:.2f}" formatted # >>> '1.13' In some Python versions such as 2.6 and 3.0, you must specify positional indexes in the format string: # ValueError in python 2.6 and 3.0 a=1 b=2 "{}-{}".format(a,b) # NO ERROR in any python version "{0}-{1}".format(a,b) # >>> "1-2" Python 3 docs: Format Specification Mini Language ยท
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-round-to-two-decimal-places
How to Round to 2 Decimal Places in Python | DataCamp
August 8, 2024 - Learn how to round a number to two decimal places in Python for improved precision using techniques like round(), format(), and string formatting techniques.