How to display a float with two decimal places?

PythonStringFloating Point

Python Problem Overview


I have a function taking float arguments (generally integers or decimals with one significant digit), and I need to output the values in a string with two decimal places (5 → 5.00, 5.5 → 5.50, etc). How can I do this in Python?

Python Solutions


Solution 1 - Python

Since this post might be here for a while, lets also point out python 3 syntax:

"{:.2f}".format(5)

Solution 2 - Python

You could use the string formatting operator for that:

>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'

The result of the operator is a string, so you can store it in a variable, print etc.

Solution 3 - Python

f-string formatting:

This was new in Python 3.6 - the string is placed in quotation marks as usual, prepended with f'... in the same way you would r'... for a raw string. Then you place whatever you want to put within your string, variables, numbers, inside braces f'some string text with a {variable} or {number} within that text' - and Python evaluates as with previous string formatting methods, except that this method is much more readable.

>>> foobar = 3.141592
>>> print(f'My number is {foobar:.2f} - look at the nice rounding!')

My number is 3.14 - look at the nice rounding!

You can see in this example we format with decimal places in similar fashion to previous string formatting methods.

NB foobar can be an number, variable, or even an expression eg f'{3*my_func(3.14):02f}'.

Going forward, with new code I prefer f-strings over common %s or str.format() methods as f-strings can be far more readable, and are often much faster.

Solution 4 - Python

String Formatting:

a = 6.789809823
print('%.2f' %a)

OR

print ("{0:.2f}".format(a)) 

Round Function can be used:

print(round(a, 2))

Good thing about round() is that, we can store this result to another variable, and then use it for other purposes.

b = round(a, 2)
print(b)

Use round() - mostly for display purpose.

Solution 5 - Python

String formatting:

print "%.2f" % 5

Solution 6 - Python

Using python string formatting.

>>> "%0.2f" % 3
'3.00'

Solution 7 - Python

If you actually want to change the number itself instead of only displaying it differently use format()

Format it to 2 decimal places:

format(value, '.2f')

example:

>>> format(5.00000, '.2f')
'5.00'

Solution 8 - Python

Shortest Python 3 syntax:

n = 5
print(f'{n:.2f}')

Solution 9 - Python

In Python 3

print(f"{number:.2f}")

A shorter way to do format.

Solution 10 - Python

I know it is an old question, but I was struggling finding the answer myself. Here is what I have come up with:

Python 3:

>>> num_dict = {'num': 0.123, 'num2': 0.127}
>>> "{0[num]:.2f}_{0[num2]:.2f}".format(num_dict) 
0.12_0.13

Solution 11 - Python

Using Python 3 syntax:

print('%.2f' % number)

Solution 12 - Python

If you want to get a floating point value with two decimal places limited at the time of calling input,

Check this out ~

a = eval(format(float(input()), '.2f'))   # if u feed 3.1415 for 'a'.
print(a)                                  # output 3.14 will be printed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarathonStudiosView Question on Stackoverflow
Solution 1 - PythonDaren ThomasView Answer on Stackoverflow
Solution 2 - PythonNPEView Answer on Stackoverflow
Solution 3 - PythontoonarmycaptainView Answer on Stackoverflow
Solution 4 - Pythondebaonline4uView Answer on Stackoverflow
Solution 5 - Pythonbradley.ayersView Answer on Stackoverflow
Solution 6 - PythonkevpieView Answer on Stackoverflow
Solution 7 - PythonJoshView Answer on Stackoverflow
Solution 8 - PythonDosView Answer on Stackoverflow
Solution 9 - PythonoplxyView Answer on Stackoverflow
Solution 10 - PythonVasiliy TriandafilidiView Answer on Stackoverflow
Solution 11 - PythonS. RoelofsView Answer on Stackoverflow
Solution 12 - Pythonkarthik ElangoView Answer on Stackoverflow