Skip to content

Instantly share code, notes, and snippets.

@mariorz
Created January 16, 2010 20:24
Show Gist options
  • Save mariorz/278987 to your computer and use it in GitHub Desktop.
Save mariorz/278987 to your computer and use it in GitHub Desktop.
def commafy(num):
return splitThousands(str(num), ',', '.')
def splitThousands(s, tSep, dSep=None):
if s.rfind('.')>0:
rhs=s[s.rfind('.')+1:]
s=s[:s.rfind('.')-1]
if len(s) <= 3: return s + dSep + rhs
return splitThousands(s[:-3], tSep) + tSep + s[-3:] + dSep + rhs
else:
if len(s) <= 3: return s
return splitThousands(s[:-3], tSep) + tSep + s[-3:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment