Skip to content

Instantly share code, notes, and snippets.

@javierwilson
Created March 15, 2012 01:28
Show Gist options
  • Save javierwilson/2041028 to your computer and use it in GitHub Desktop.
Save javierwilson/2041028 to your computer and use it in GitHub Desktop.
solo usa decimales si es necesario y usa mil, millon
def number_format(x=0, precision=2, currency="US$ "):
x = Decimal('%.4f' % x)
fformat = '%.' + str(precision) + 'f'
nombres = ['', ' mil', ' millones']
if x < 1000:
s = (fformat % x).rstrip('0').rstrip('.')
s = s + nombres[0]
if 1000 <= x < 1000000:
s = (fformat % (x / 1000)).rstrip('0').rstrip('.')
s = s + nombres[1]
if x >= 1000000:
s = (fformat % (x / 1000000)).rstrip('0').rstrip('.')
s = s + nombres[2]
return currency + s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment