user
user

Reputation: 1331

changing float format to have both decimals and commas

For example, how would do you make 1003.2 format to 1,003.20?

I know I can use '%.2f' % 1003.2 to make it have two decimal places, but how do you also make the comma appear?

Upvotes: 1

Views: 129

Answers (1)

falsetru
falsetru

Reputation: 369054

Using str.format:

>>> '{:,.2f}'.format(1003.2)
'1,003.20'

The ',' format specification signals the use of a comma for a thousands separator.

Upvotes: 2

Related Questions