FAQ
Hi,

I have an SQL query which I can format to give me a nice value like
23,342.40 which Python squashes into 23342.4 when I stick it into a
list element. Is there a nice way to format floats so they look like
(US) currency?

Thanks,

Joe

Search Discussions

  • Ken Seehof at Jun 1, 2000 at 11:04 pm

    x = 34.9
    s = "$%.2f" % x
    print s
    $34.90

    Read more in "More String Operations" in the "Python Library Reference"

    Joseph Santaniello wrote:
    Hi,

    I have an SQL query which I can format to give me a nice value like
    23,342.40 which Python squashes into 23342.4 when I stick it into a
    list element. Is there a nice way to format floats so they look like
    (US) currency?

    Thanks,

    Joe
  • Joseph Santaniello at Jun 1, 2000 at 11:58 pm
    Ah, yes. But I'm still missing my commas... A nice clean way to do that?

    Thanks for the tip!

    Joe
    On Thu, 1 Jun 2000, Ken Seehof wrote:

    x = 34.9
    s = "$%.2f" % x
    print s
    $34.90

    Read more in "More String Operations" in the "Python Library Reference"

    Joseph Santaniello wrote:
    Hi,

    I have an SQL query which I can format to give me a nice value like
    23,342.40 which Python squashes into 23342.4 when I stick it into a
    list element. Is there a nice way to format floats so they look like
    (US) currency?

    Thanks,

    Joe
  • Andrew Dalke at Jun 2, 2000 at 3:53 am

    Joseph Santaniello wrote:
    Ah, yes. But I'm still missing my commas [for 23,342.40]... A nice
    clean way to do that?
    I think the following is only somewhat tricky, but perhaps the
    cleanest. (Okay, there's also supposed to be a way to do this with
    locale, but I don't know how.)

    def dollar(f):
    if f < 0: # pull the sign out now so I know where to stop
    sign = "-" # when going bacwards
    f = -f
    else:
    sign = ""

    s = "%.2f" % f
    # -6 is the thousand's place -- 987654.21
    # -3 goes back 1000 at a time
    # stop at 0 instead of -1 so "999.99" doesn't lead with a ",".
    for i in range(len(s)-6, 0, -3): # -6 is the first thousands place
    s = s[:i] + "," + s[i:] # Go back 1000 at a time
    return sign + s
    for i in (0, .9, 1, 10, 99.99, 100.00, 100.0, 999.99, 1000.,
    1000.00, 1000000000L, -0.01, -0.9, -1, -99.9, -100, -999.99, -1000.00,
    -123456789):
    ... print i, dollar(i)
    ...
    0 0.00
    0.9 0.90
    1 1.00
    10 10.00
    99.99 99.99
    100.0 100.00
    100.0 100.00
    999.99 999.99
    1000.0 1,000.00
    1000.0 1,000.00
    1000000000L 1,000,000,000.00
    -0.01 -0.01
    -0.9 -0.90
    -1 -1.00
    -99.9 -99.90
    -100 -100.00
    -999.99 -999.99
    -1000.0 -1,000.00
    -123456789 -123,456,789.00

    Andrew
    dalke at acm.org
  • Joseph Santaniello at Jun 2, 2000 at 4:29 pm
    Nice!

    I gave up on a locale way, but this is just what I needed. Thanks
    everybody!

    Joe
    On Thu, 1 Jun 2000, Andrew Dalke wrote:


    Joseph Santaniello wrote:
    Ah, yes. But I'm still missing my commas [for 23,342.40]... A nice
    clean way to do that?
    I think the following is only somewhat tricky, but perhaps the
    cleanest. (Okay, there's also supposed to be a way to do this with
    locale, but I don't know how.)

    def dollar(f):
    if f < 0: # pull the sign out now so I know where to stop
    sign = "-" # when going bacwards
    f = -f
    else:
    sign = ""

    s = "%.2f" % f
    # -6 is the thousand's place -- 987654.21
    # -3 goes back 1000 at a time
    # stop at 0 instead of -1 so "999.99" doesn't lead with a ",".
    for i in range(len(s)-6, 0, -3): # -6 is the first thousands place
    s = s[:i] + "," + s[i:] # Go back 1000 at a time
    return sign + s
    for i in (0, .9, 1, 10, 99.99, 100.00, 100.0, 999.99, 1000.,
    1000.00, 1000000000L, -0.01, -0.9, -1, -99.9, -100, -999.99, -1000.00,
    -123456789):
    ... print i, dollar(i)
    ...
    0 0.00
    0.9 0.90
    1 1.00
    10 10.00
    99.99 99.99
    100.0 100.00
    100.0 100.00
    999.99 999.99
    1000.0 1,000.00
    1000.0 1,000.00
    1000000000L 1,000,000,000.00
    -0.01 -0.01
    -0.9 -0.90
    -1 -1.00
    -99.9 -99.90
    -100 -100.00
    -999.99 -999.99
    -1000.0 -1,000.00
    -123456789 -123,456,789.00

    Andrew
    dalke at acm.org


  • Neel Krishnaswami at Jun 2, 2000 at 11:08 pm

    Joseph Santaniello wrote:
    I gave up on a locale way, but this is just what I needed. Thanks
    everybody!
    The original post didn't show up on my machine, so I'm responding
    to this followup.

    Let me caution you against using floating point numbers when dealing
    with money amounts -- the rounding errors that can accumulate can
    make reconciling your accounting records a PITA.

    You are much better off storing money as an integer, denominated in
    pennies (or whatever the equivalent minimal integral unit is for the
    kroner). Or, if you really want to apparently use floating point, I'd
    use a real fixed-point arithmetic class, an implementation of which
    Tim Peters posted to the list in the past year. (Deja's archives are
    down, as is the python.org search, or I'd give you a URL to
    it. Hopefully Tim will followup with a repost....)



    Neel
  • Bjorn Pettersen at Jun 2, 2000 at 9:28 pm

    Andrew Dalke wrote:
    Joseph Santaniello wrote:
    Ah, yes. But I'm still missing my commas [for 23,342.40]... A nice
    clean way to do that?
    I think the following is only somewhat tricky, but perhaps the
    cleanest. (Okay, there's also supposed to be a way to do this with
    locale, but I don't know how.)
    import locale
    locale.setlocale(locale.LC_ALL,"") # set to default locale
    locale.format('%.2f', 5555.555, 3) # (format, value, grouping)
    # outputs 5,555.56 on my system

    --bjorn
  • Wjk at Jun 2, 2000 at 7:53 am
    I was playing around learning Python a few months ago and have
    started the learning adventure again. I was interested in the same
    question and approached it from a slightly different angle -- the code
    while not yet complete might be of some interest....


    import re
    pattern=re.compile("[0-9]{4,4}[\.\,$]")

    #something here to insure 2 decimal places

    test3567789.98
    teststr=str(test)
    a = pattern.search(teststr)
    while a:
    (a.regs[0])[0] + 1
    teststr = teststr[0:(a.regs[0])[0] + 1] + "," + teststr[(a.regs[0])[0]
    + 1:]
    a = pattern.search(teststr)

    #possibly something here to convert to float if
    #to be used for mathematical computations

    print teststr




    Joseph Santaniello wrote:
    Hi,

    I have an SQL query which I can format to give me a nice value like
    23,342.40 which Python squashes into 23342.4 when I stick it into a
    list element. Is there a nice way to format floats so they look like
    (US) currency?

    Thanks,

    Joe
  • Russell Wallace at Jun 2, 2000 at 3:21 pm

    Joseph Santaniello wrote:
    Hi,

    I have an SQL query which I can format to give me a nice value like
    23,342.40 which Python squashes into 23342.4 when I stick it into a
    list element. Is there a nice way to format floats so they look like
    (US) currency?

    Thanks,

    Joe
    I've written a formatting routine that does this. (For physical
    quantities, it can also round to a specified number of significant
    figures irrespective of where the decimal point is.) Let me know if you
    want a copy.

    Is there a central repository for these sort of things?

    --
    "To summarize the summary of the summary: people are a problem."
    Russell Wallace
    mailto:rwallace at esatclear.ie
    http://www.esatclear.ie/~rwallace
  • Jepler epler at Jun 2, 2000 at 11:17 pm

    On Thu, 1 Jun 2000 19:20:31 +0200, Joseph Santaniello wrote:
    Hi,

    I have an SQL query which I can format to give me a nice value like
    23,342.40 which Python squashes into 23342.4 when I stick it into a
    list element. Is there a nice way to format floats so they look like
    (US) currency?
    According to the Linux manpage for printf(3), the following flag
    can be included in a format specifier:
    ' specifying that in a numerical argument the
    output is to be grouped if the locale infor-
    mation indicates any. Note that many ver-
    sions of gcc cannot parse this option and
    will issue a warning.
    The manpage doesn't note that this is a nonstandard extension to printf
    (as it does for the use of q, ll, and L), so I am lead to understand
    that this conforms to ANSI C3.159-1989 (``ANSI C'').

    However, 1.6a1 gives:
    print "%'f" % 66666.666
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: unsupported format character ''' (0x27)
    i.e., no go---this isn't directly supported yet.

    If you have locale enabled, and I don't, it looks like locale.format
    will return a string in the desired format, and local.ato[if] will make
    that string into an integer/float again.

    These techniques, to the extent that they work, will make your program
    behave correctly in *all* locales (or at least, more than one), for
    instance those which use . for the thousands separator and , for the
    decimal point.

    Jeff

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 1, '00 at 5:20p
activeJun 2, '00 at 11:17p
posts10
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase