Concatenated numerical literals

Discussion in 'Python' started by Per Erik Stendahl, Mar 1, 2004.

  1. Hi,

    I wish I could write large integers (and floats, etc) like
    this in Python:

    10 000 000

    Kind of the same way I can write string literals today.
    (As in 'This' 'is' 'one' 'string'.)

    Has there been any discussion on this? I find myself wishing
    for this at least once a week, since it really helps readability.
    It doesn't seem like a big implementation either.

    Any comments?

    Regards,
    Per Erik Stendahl,
    Uppsala, Sweden
    Per Erik Stendahl, Mar 1, 2004
    #1
    1. Advertising

  2. Per Erik Stendahl

    Larry Bates Guest

    I took a function that I wrote to format floats with
    commas and currency symbol and hacked it a little to make
    it work with your spaces. You should be able to take it
    and with little modifications make it work for you.

    -Larry

    def fmt_currency(amount):
    import types, string
    '''
    This function will take the number passed to it and format it as US
    Currency with floating $ sign, comma separators and two decimal places
    '''
    thousands_sep=" "
    currency="$"
    #
    # Check to see if I got a number or a string
    #
    if type(amount) == types.StringType:
    #
    # Check to see if I got an empty string, return $0.00
    #
    if amount == '': amount=0
    else: amount=string.atof(amount)

    temp="%.2f" % amount
    outstring=temp[len(temp)-4:]
    if len(temp)==4: return currency+outstring
    digits=list(temp[0:len(temp)-4])
    digits.reverse()
    #print digits
    cpos=1
    for c in digits:
    outstring=c+outstring
    cpos=cpos+1
    if cpos%3 == 0: outstring=thousands_sep+outstring
    #
    # Check to see if comma is first character of the formatted string
    # if it is strip it off.
    #
    if outstring.startswith(thousands_sep): outstring=outstring[1:]
    return currency+outstring

    if __name__=="__main__":
    print fmt_currency(0.12)
    print fmt_currency(123.45)
    print fmt_currency(123456789.01)


    "Per Erik Stendahl" <> wrote in message
    news:...
    > Hi,
    >
    > I wish I could write large integers (and floats, etc) like
    > this in Python:
    >
    > 10 000 000
    >
    > Kind of the same way I can write string literals today.
    > (As in 'This' 'is' 'one' 'string'.)
    >
    > Has there been any discussion on this? I find myself wishing
    > for this at least once a week, since it really helps readability.
    > It doesn't seem like a big implementation either.
    >
    > Any comments?
    >
    > Regards,
    > Per Erik Stendahl,
    > Uppsala, Sweden
    >
    Larry Bates, Mar 1, 2004
    #2
    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. dh
    Replies:
    0
    Views:
    330
  2. John Goche
    Replies:
    8
    Views:
    16,454
  3. Shabam

    Form variable concatenated

    Shabam, Sep 16, 2005, in forum: HTML
    Replies:
    2
    Views:
    404
    Mark Parnell
    Sep 16, 2005
  4. dejan budimir
    Replies:
    6
    Views:
    370
    Rolf Magnus
    Jul 4, 2004
  5. Prateek R Karandikar
    Replies:
    11
    Views:
    585
Loading...

Share This Page