From 832176181cd26f3ebdbedec03aa9a2fffe963553 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 19 Jun 2008 16:23:08 +0200 Subject: [PATCH] add oxutils.formatCurrency --- oxutils/format.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/oxutils/format.py b/oxutils/format.py index 252db09..bfb2ca7 100644 --- a/oxutils/format.py +++ b/oxutils/format.py @@ -112,6 +112,20 @@ def formatBytes(number): def formatPixels(number): return formatNumber(number, 'pixel', 'px') +def formatCurrency(amount, currency="$"): + if amount: + temp = "%.2f" % amount + profile=re.compile(r"(\d)(\d\d\d[.,])") + while 1: + temp, count = re.subn(profile,r"\1,\2",temp) + if not count: + break + if temp.startswith('-'): + return "-"+ currency + temp[1:-3] + return currency + temp[:-3] + else: + return "" + def plural(amount, unit, plural='s'): ''' >>> plural(1, 'unit')