Talk:Common Invention Recipes

From Unofficial Homecoming Wiki
Revision as of 13:18, 18 April 2007 by imported>Lin Chiao Feng (Note to self)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Python Stuff

Note to self:

def commify(amount):
    amount = str(amount).strip()
    try:
        whole, fract = amount.split('.', 1)
        fract = '.' + fract
    except ValueError:
        whole = amount
        fract = ''
    firstcomma = len(whole)%3 or 3  # set to 3 if would make a leading comma
    first, rest = whole[:firstcomma], whole[firstcomma:]
    segments = [first] + [rest[i:i+3] for i in range(0, len(rest), 3)]
    return ",".join(segments) + fract

Based on code from http://pleac.sourceforge.net/pleac_python/numbers.html. (Added code to handle decimals.) --Lin Chiao Feng 06:18, 18 April 2007 (PDT)