Over a million developers have joined DZone.

Adding Commas To Digits

·
Copy from Marek Baczynski's code here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439357

def group(n, sep = ','):
    s = str(n)[::-1]
    groups = []
    i = 0
    while i < len(s):
        groups.append(s[i:i+3])
        i += 3
    return sep.join(groups)[::-1]

#>>> group(42424242)
#'42,424,242'

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}