--- moneyfmt.py 2008-04-17 11:45:35.182548618 +0200 +++ moneyfmt_new.py 2008-04-17 11:45:40.182833577 +0200 @@ -1,5 +1,5 @@ def moneyfmt(value, places=2, curr='', sep=',', dp='.', - pos='', neg='-', trailneg=''): + pos='', neg='-', trailneg='', zero=''): """Convert Decimal to a money formatted string. places: required number of places after the decimal point @@ -10,6 +10,8 @@ pos: optional sign for positive numbers: '+', space or blank neg: optional sign for negative numbers: '-', '(', space or blank trailneg:optional trailing minus indicator: '-', ')', space or blank + zero: optional leading character before the decimal point + for 1 < value < -1: '0' >>> d = Decimal('-1234567.8901') >>> moneyfmt(d, curr='$') @@ -22,6 +24,8 @@ '123 456 789.00' >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>') '<.02>' + >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>', zero='0') + '<0.02>' """ q = Decimal((0, (1,), -places)) # 2 places --> '0.01' @@ -39,6 +43,8 @@ build('0') build(dp) i = 0 + if not digits and zero: + build(zero) while digits: build(next()) i += 1