Badge time.png   The Paragon Wiki Archive documents the state of City of Heroes/Villains as it existed on December 1, 2012.

Talk:Common Invention Recipes

From Paragon Wiki Archive
Revision as of 13:18, 18 April 2007 by Lin Chiao Feng (Talk | contribs) (Note to self)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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)