36

I'm sure this must be a duplicate but I can't find a clear answer on SO.

How do I output 2083525.34561 as 2,083,525.35 in Python 2?

I know about:

"{0:,f}".format(2083525.34561)

which outputs commas but does not round. And:

"%.2f" % 2083525.34561

which rounds, but does not add commas.

RoadieRich
  • 6,330
  • 3
  • 35
  • 52
Richard
  • 62,943
  • 126
  • 334
  • 542

2 Answers2

66

Add a decimal point with number of digits .2f see the docs: https://docs.python.org/2/library/string.html#format-specification-mini-language :

In [212]:
"{0:,.2f}".format(2083525.34561)

Out[212]:
'2,083,525.35'

For python 3 you can use f-strings (thanks to @Alex F):

In [2]:
value = 2083525.34561
f"{value:,.2f}"


Out[2]:
'2,083,525.35'
EdChum
  • 376,765
  • 198
  • 813
  • 562
-1
("%.2f" % 2083525.34561).replace(".", ",")
Vincent
  • 1,016
  • 6
  • 11