Created
March 15, 2012 01:28
-
-
Save javierwilson/2041028 to your computer and use it in GitHub Desktop.
solo usa decimales si es necesario y usa mil, millon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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