15

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
1

3 Answers 3

35

Use format:

>>> bankBalance = 1000000
>>> format(bankBalance, ",")
'1,000,000'
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing find...unnoticed for years.
6

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

>>> '{:,}'.format(12345)
'12,345'
>>> format(12345, ',')
'12,345'

Comments

2

Using format:

>>> "{:,}".format(1000000)
'1,000,000'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.