howto format currency string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adegreiff

    howto format currency string?

    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_comma s function but that returned
    "10,250.1" which is wrong.

    any ideas?

    thanks
    alexander
  • Alex Martelli

    #2
    Re: howto format currency string?

    adegreiff wrote:
    [color=blue]
    > 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_comma s function but that returned
    > "10,250.1" which is wrong.
    >
    > any ideas?[/color]
    [color=blue][color=green][color=darkred]
    >>> import locale
    >>> locale.setlocal e(locale.LC_ALL ,('en','ascii') )[/color][/color][/color]
    'en_US.ISO8859-1'[color=blue][color=green][color=darkred]
    >>> locale.format(' %.2f', 10250.10, True)[/color][/color][/color]
    '10,250.10'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    Alex

    Comment

    • Peter Otten

      #3
      Re: howto format currency string?


      Alex Martelli wrote:
      [color=blue][color=green][color=darkred]
      >>>> import locale
      >>>> locale.setlocal e(locale.LC_ALL ,('en','ascii') )[/color][/color]
      > 'en_US.ISO8859-1'[/color]


      Python 2.3 (#1, Jul 30 2003, 11:19:43)
      [GCC 3.2] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
      >>> import locale
      >>> locale.setlocal e(locale.LC_ALL , ('de', None))[/color][/color][/color]
      'de_DE.ISO8859-1'

      It works :-)
      And I always trusted the documentation/error message:
      [color=blue][color=green][color=darkred]
      >>> locale.setlocal e(locale.LC_ALL , 'de')[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "locale.py" , line 381, in setlocale
      return _setlocale(cate gory, locale)
      locale.Error: locale setting not supported[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      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

      Comment

      • Alex Martelli

        #4
        Re: howto format currency string?

        Peter Otten wrote:
        ...[color=blue][color=green][color=darkred]
        >>>> locale.setlocal e(locale.LC_ALL , 'de')[/color][/color]
        > Traceback (most recent call last):
        > File "<stdin>", line 1, in ?
        > File "locale.py" , line 381, in setlocale
        > return _setlocale(cate gory, locale)
        > locale.Error: locale setting not supported[color=green][color=darkred]
        >>>>[/color][/color]
        >
        > 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?[/color]

        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

        Comment

        Working...