A useful, but painful, one-liner to edit money amounts

DarkBlue pict100 at gmail.com
Thu Aug 5 13:56:38 CEST 2010

On Aug 5, 7:06 pm, Chris Withers <ch... at simplistix.co.uk> wrote:
> Peter Otten wrote:
> >>>> locale.setlocale(locale.LC_ALL, ("en_US", "UTF-8"))
> > 'en_US.UTF8'
> >>>> print locale.currency(13535, grouping=True)
> > $13,535.00
> Okay, so if I'm writing a wsgi app, and I want to format depending on
> the choices of the currently logged in users, what would you recommend?
> I can't do setlocale, since that would affect all users, and in a
> mult-threaded environment that would be bad.
> Does that mean the whole locale package is useless to all web-app builders?
> Chris

from re import *

class editmoney(float):
    def __init__(self,mymoney):
        self.mymoney = mymoney
    def __str__(self):
        temp = "%.2f" % self.mymoney
        profile = compile(r"(\d)(\d\d\d[.,])")
        while 1:
            temp, count = subn(profile,r"\1,\2",temp)
            if not count: break
        return temp

More information about the Python-list mailing list