How to print number with commas as thousands separ

2018-12-31 07:53发布

I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567 as 1,234,567. How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way.

It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible.

26条回答
浅入江南
2楼-- · 2018-12-31 08:15

If you don't want to depend on any external libraries:

 s = str(1234567)
 print ','.join([s[::-1][k:k+3][::-1] for k in xrange(len(s)-1, -1, -3)])

This works only for non-negative integers.

查看更多
听够珍惜
3楼-- · 2018-12-31 08:17

For Python ≥ 2.7:

"{:,}".format(value)

Per Format Specification Mini-Language,

The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

查看更多
梦该遗忘
4楼-- · 2018-12-31 08:19

I have a python 2 and python 3 version of this code. I know that the question was asked for python 2 but now (8 years later lol) people will probably be using python 3.

Python 3 Code:

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print('The original number is: {}. '.format(number))
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print('The new and improved number is: {}'.format(number))        


Python 2 Code: (Edit. The python 2 code isn't working. I am thinking that the syntax is different).

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print 'The original number is: %s.' % (number)
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print 'The new and improved number is: %s.' % (number) 
查看更多
闭嘴吧你
5楼-- · 2018-12-31 08:21

For floats:

float(filter(lambda x: x!=',', '1,234.52'))
# returns 1234.52

For ints:

int(filter(lambda x: x!=',', '1,234'))
# returns 1234
查看更多
孤独寂梦人
6楼-- · 2018-12-31 08:22

I'm surprised that no one has mentioned that you can do this with f-strings in Python 3.6 as easy as this:

>>> num = 10000000
>>> print(f"{num:,d}")
10,000,000

... where the part after the colon is the format specifier. The comma is the separator character you want, so f"{num:_d}" uses underscores instead of a comma.

This is equivalent of using format(num, ",d") for older versions of python 3.

查看更多
何处买醉
7楼-- · 2018-12-31 08:23

The accepted answer is fine, but I actually prefer format(number,','). Easier for me to interpret and remember.

https://docs.python.org/3/library/functions.html#format

查看更多
登录 后发表回答