11

I'm currently working on a program and I've run into a problem.

I have a bank balance of 1000000 but when I display it on the screen I want it to read as "1,000,000".

Now there are ways of getting around this simply by having it set to "1,000,000" and then striping it of commas and converting it to an integer when I need to use the integer value. But I don't want to do that.

bankBalance = 1000000
  • See also https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators. – flow2k Aug 03 '20 at 23:19

3 Answers3

30

Use format:

>>> bankBalance = 1000000
>>> format(bankBalance, ",")
'1,000,000'
>>>
5

Using str.format or format with , as format specifier:

>>> '{:,}'.format(12345)
'12,345'
>>> format(12345, ',')
'12,345'
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

Using format:

>>> "{:,}".format(1000000)
'1,000,000'
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202