howto format currency string?

Discussion in 'Python' started by adegreiff, Aug 29, 2003.

  1. adegreiff

    adegreiff Guest

    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
    adegreiff, Aug 29, 2003
    #1
    1. Advertising

  2. 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
    Alex Martelli, Aug 29, 2003
    #2
    1. Advertising

  3. adegreiff

    Peter Otten Guest

    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
    Peter Otten, Aug 29, 2003
    #3
  4. 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
    Alex Martelli, Aug 29, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Biao Tang
    Replies:
    0
    Views:
    464
    Biao Tang
    Apr 28, 2005
  2. TJS
    Replies:
    0
    Views:
    476
  3. D. Shane Fowlkes

    Format DataItem 'inline' as Currency/String?

    D. Shane Fowlkes, Jun 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,902
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Jun 9, 2005
  4. VB Programmer

    Format Currency for label

    VB Programmer, Sep 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,088
    VB Programmer
    Sep 14, 2005
  5. Joshua

    Format String to Currency

    Joshua, Sep 7, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    513
    Mark Rae [MVP]
    Sep 7, 2007
Loading...

Share This Page