[Tutor] Even More Converter!

Kent Johnson kent37 at tds.net
Sat Mar 22 14:41:34 CET 2008


wackedd at mac.com wrote:
> When Python gives me the answer to my conversion, is there a way to create it so every 3 numbers a comma is inserted?

Django uses this function:
def intcomma(value):
     """
     Converts an integer to a string containing commas every three digits.
     For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
     """
     orig = str(value)
     new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', str(value))
     if orig == new:
         return new
     else:
         return intcomma(new)

> Also is there a way to make this so I don't have to go through every individual line of code and add *insert comma* or something to it, simply at the top like how the Unit Menu is placed only once there, and yet applies to the whole document. Thank you

No. The way to do this is to learn how to use functions to consolidate 
all the prints into one place.

Kent


More information about the Tutor mailing list