Display a float with two decimal places in Python

MarathonStudios picture MarathonStudios · May 27, 2011 · Viewed 342.8k times · Source

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?

Answer

Daren Thomas picture Daren Thomas · May 27, 2011

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

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