formatting a string with thousands separators

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

    formatting a string with thousands separators

    anyone recommend a way of formatting floats with comma separators?

    e.g. 500000.00 -500,000.00
  • Alan G Isaac

    #2
    Re: formatting a string with thousands separators

    On 9/7/2008 12:22 PM SimonPalmer apparently wrote:
    anyone recommend a way of formatting floats with comma separators?



    Alan Isaac

    Comment

    • James Mills

      #3
      Re: formatting a string with thousands separators

      Hi,

      There is a much easier more consistent way:
      >>import locale
      >>locale.setloc ale(locale.LC_A LL, "en_AU.UTF-8")
      'en_AU.UTF-8'
      >>locale.format ("%0.2f", 5000000, True)
      '5,000,000.00'
      >>>
      cheers
      James

      On Mon, Sep 8, 2008 at 5:24 AM, Alan G Isaac <alan.isaac@gma il.comwrote:
      On 9/7/2008 12:22 PM SimonPalmer apparently wrote:
      >>
      >anyone recommend a way of formatting floats with comma separators?
      >
      >

      >
      Alan Isaac
      --

      >


      --
      --
      -- "Problems are solved by method"

      Comment

      • Fredrik Lundh

        #4
        Re: formatting a string with thousands separators

        James Mills wrote:
        There is a much easier more consistent way:
        >
        >>>import locale
        >>>locale.setlo cale(locale.LC_ ALL, "en_AU.UTF-8")
        'en_AU.UTF-8'
        doesn't work on all Python platforms, though:
        >>locale.setloc ale(locale.LC_A LL, "en_AU.UTF-8")
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "c:\python25\li b\locale.py", line 478, in setlocale
        return _setlocale(cate gory, locale)
        locale.Error: unsupported locale setting

        and is likely to give you somewhat unpredictable output if you use the
        machine's actual locale:
        >>import os
        >>locale.setloc ale(locale.LC_A LL, os.environ["LANG"])
        'sv_SE.UTF-8'
        >>locale.format ("%0.2f", 5000000, True)
        '5000000,00'

        </F>

        Comment

        Working...