Python Add Comma Into Number String

前端 未结 9 1065
陌清茗
陌清茗 2020-11-29 02:37

Using Python v2, I have a value running through my program that puts out a number rounded to 2 decimal places at the end:

like this:

print (\"Total          


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 03:10

    You could use locale.currency if TotalAmount represents money. It works on Python <2.7 too:

    >>> locale.setlocale(locale.LC_ALL, '')
    'en_US.utf8'
    >>> locale.currency(123456.789, symbol=False, grouping=True)
    '123,456.79'
    

    Note: it doesn't work with the C locale so you should set some other locale before calling it.

提交回复
热议问题