|
Academic Work Personal
|
Python /
Convert a number to a nicely formatted string
Migrated to http://pythonadventures.wordpress.com/2010/09/24/pretty-print-int/.
#!/usr/bin/env python
def numberToPrettyString(n):
"""Converts a number to a nicely formatted string.
Example: 6874 => '6,874'."""
l = []
for i, c in enumerate(str(n)[::-1]):
if i%3==0 and i!=0:
l += ','
l += c
return "".join(l[::-1])
#
if __name__ == "__main__":
number = 6874
print numberToPrettyString(number) # '6,874'
|
![]() anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows Blogs and Dev. * Ubuntu Incident Places Debrecen | France | Hungary | Montreal | Nancy Notes Hobby Projects * Jabba's Codes Quick Links [ edit ] |