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

Paul Rubin no.email at nospam.invalid
Thu Aug 5 08:03:02 CEST 2010


John Nagle <nagle at animats.com> writes:
> def editmoney(n) :
>     return((",".join(reduce(lambda lst, item : (lst + [item]) if
>         item else lst,
>         re.split(r'(\d\d\d)',str(n)[::-1]),[])))[::-1])

Too obscure.  I usually use something like this:

    def editmoney(n):
        if n < 0: return '-' + editmoney(-n)
        if n >= 1000:
            return editmoney(n // 1000) + ',%03d'% (n % 1000)
        return '%d'% n



More information about the Python-list mailing list