Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > howto format currency string?

Reply
Thread Tools

howto format currency string?

 
 
adegreiff
Guest
Posts: n/a
 
      08-29-2003
hi,

using zope/pythons/page templates i need to format a float number to a
string like 10,250.10

(note the thousand separator and the trailing zero in the decimal)

i was able to use %0.2f bit that returned no thousand separator.
i was able to use thousands_commas function but that returned
"10,250.1" which is wrong.

any ideas?

thanks
alexander
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      08-29-2003
adegreiff wrote:

> hi,
>
> using zope/pythons/page templates i need to format a float number to a
> string like 10,250.10
>
> (note the thousand separator and the trailing zero in the decimal)
>
> i was able to use %0.2f bit that returned no thousand separator.
> i was able to use thousands_commas function but that returned
> "10,250.1" which is wrong.
>
> any ideas?


>>> import locale
>>> locale.setlocale(locale.LC_ALL,('en','ascii'))

'en_US.ISO8859-1'
>>> locale.format('%.2f', 10250.10, True)

'10,250.10'
>>>



Alex

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-29-2003

Alex Martelli wrote:

>>>> import locale
>>>> locale.setlocale(locale.LC_ALL,('en','ascii'))

> 'en_US.ISO8859-1'



Python 2.3 (#1, Jul 30 2003, 11:19:43)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, ('de', None))

'de_DE.ISO8859-1'

It works
And I always trusted the documentation/error message:

>>> locale.setlocale(locale.LC_ALL, 'de')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "locale.py", line 381, in setlocale
return _setlocale(category, locale)
locale.Error: locale setting not supported
>>>


The second (not working) variant is taken directly from the documentation.
The error message led me to assume that my version of Python didn't support
locale setting at all.

Maybe I should file a bug report?

Peter

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      08-29-2003
Peter Otten wrote:
...
>>>> locale.setlocale(locale.LC_ALL, 'de')

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "locale.py", line 381, in setlocale
> return _setlocale(category, locale)
> locale.Error: locale setting not supported
>>>>

>
> The second (not working) variant is taken directly from the documentation.
> The error message led me to assume that my version of Python didn't
> support locale setting at all.
>
> Maybe I should file a bug report?


Yes, I believe that the error message is potentially misleading, and
filing a bug report is a good way to have it fixed ASAP, thanks.


Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I Format Currency w/out decimals? JeffP@Laptop ASP .Net 3 12-15-2005 02:06 AM
Format Currency for label VB Programmer ASP .Net 2 09-14-2005 02:35 PM
Format DataItem 'inline' as Currency/String? D. Shane Fowlkes ASP .Net 1 06-09-2005 04:05 PM
convert output to currency format TJS ASP .Net 0 05-18-2005 09:04 PM
CompareValidator on currency figure in French format Biao Tang ASP .Net 0 04-28-2005 09:15 AM



Advertisments