printing float as formatted dollars
Peter Hansen
peter at engcorp.com
Fri May 7 07:31:59 EDT 2004
Tim wrote:
> "Larry Bates" <lbates at swamisoft.com> wrote in message
> news:YtqdnZOyj_KSfArdRVn_iw at comcast.com...
>
>>Here is a function I wrote and use. Note: floats
>>have rounding "issues" when they get very large.
[snip long code]
>>
> Thanks for the code!, it was a big help. I had started trying to piece
> together some logic (below), but I know there are holes in it (negative
> numbers, for one).
[snip also long code]
I still find this one much easier to swallow (from the
aforementioned other thread).
>>> import re
>>> s = 1234567.89
>>> '$' + re.sub(r'(?<=\d)(?=(\d\d\d)+(\.|$))', ',', str(s))
'$1,234,567.89'
-Peter
More information about the Python-list
mailing list