4

With the new formatting syntax, Python can easily be made to print a thousands separator:

print ("{0:,.2f} Euro".format(myVariable))

So the comma takes care for this.

How can this be done with the old syntax - without using locale?

print ("%.2f Euro" % myVariable)
falsetru
  • 336,967
  • 57
  • 673
  • 597

1 Answers1

3

printf-style formatting does not support thousand separator.

You need to build a string with thousand separator:

>>> amount = 12345
>>> '%s Euro' % format(amount, ',.2f')
'12,345.00 Euro'

Built-in function format uses the same Format Specification Mini-Language that str.format uses.

If you can't use format, see this question for alternatives.

falsetru
  • 336,967
  • 57
  • 673
  • 597