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:
Discussions

How to format to two decimal places python
There are several ways to do it, string formatting being one, but round is the easiest: String formatting: print('%.2f' % number) round: float_number = round(number, 2) More on reddit.com
๐ŸŒ r/learnprogramming
4
1
April 29, 2021
Logging โ†’ format decimal places?
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 More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
November 8, 2023
How can I format a decimal to always show 2 decimal places in Python? - TestMu AI Community
How can I format a decimal to always show 2 decimal places in Python? I want to display values like: - 49 as 49.00 - 54.9 as 54.90 Regardless of the number of decimal places, I want to ensure that the decimal is always displayed with 2 decimal places. This is for displaying monetary values, ... More on community.testmuai.com
๐ŸŒ community.testmuai.com
0
November 30, 2024
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
0
May 19, 2024
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
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 do this manually, you need to multiply the number by one hundred and append it with a percent sign (%) before displaying it with the required amount of decimal places. You can do all of this automatically by using the % presentation type ...
๐ŸŒ
mkaz.blog
mkaz.blog โ€บ working-with-python โ€บ string-formatting
Python String Formatting: Complete Guide
F-strings support comprehensive number formatting using format specifications after a colon: 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 print(f"Center aligned: {value:^10.2f}") # 1234.57 print(f"Zero padded: {value:010.2f}") # 001234.57 # Thousands separator print(f"With commas: {value:,.2f}") # 1,234.57 # Percentage ratio = 0.857 print(f"Percentage: {ratio:.1%}") # 85.7% # Scientific notation big_number = 1500000 print(f"Scientific: {big_number:.2e}") # 1.50e+06 # Different bases num = 255 print(f"Hex: {num:x}") # ff print(f"Binary: {num:b}") # 11111111 print(f"Octal: {num:o}") # 377
๐ŸŒ
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...
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
For Decimal, the rounding mode of the current context will be used. The available presentation types for complex are the same as those for float ('%' is not allowed). Both the real and imaginary components of a complex number are formatted as ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
TestMu AI Community
community.testmuai.com โ€บ ask a question
How can I format a decimal to always show 2 decimal places in Python? - TestMu AI Community
November 30, 2024 - How can I format a decimal to always show 2 decimal places in Python? I want to display values like: - 49 as 49.00 - 54.9 as 54.90 Regardless of the number of decimal places, I want to ensure that the decimal is always displayed with 2 decimal places. This is for displaying monetary values, ...
๐ŸŒ
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 ...