Currency format for floats?

wjk wjk at wjk.mv.com
Fri Jun 2 03:53:38 EDT 2000


I was playing around learning Python a few months ago and have
started the learning adventure again.  I was interested in the same
question and approached it from a slightly different angle -- the code
while not yet complete might be of some interest....


import re
pattern=re.compile("[0-9]{4,4}[\.\,$]")

#something here to insure 2 decimal places

test=123567789.98
teststr=str(test)
a = pattern.search(teststr)
while a:
 (a.regs[0])[0] + 1
 teststr = teststr[0:(a.regs[0])[0] + 1] + "," + teststr[(a.regs[0])[0]
+ 1:]
 a = pattern.search(teststr)

#possibly something here to convert to float if
#to be used for mathematical computations

print teststr




Joseph Santaniello wrote:

> Hi,
>
> I have an SQL query which I can format to give me a nice value like
> 23,342.40 which Python squashes into 23342.4 when I stick it into a
> list element. Is there a nice way to format floats so they look like
> (US) currency?
>
> Thanks,
>
> Joe




More information about the Python-list mailing list