Re: Concatenated numerical literals

From: Larry Bates (lbates_at_swamisoft.com)
Date: 03/01/04


Date: Mon, 1 Mar 2004 10:43:13 -0600

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" <berra@psyket.com> wrote in message
news:mailman.18.1078154586.22701.python-list@python.org...
> 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
>



Relevant Pages

  • Re: CDBL Function truncates decimals
    ... Is there a way to format a number into US ... Public Function ConvertNumberforSQL (ByVal sValue As String, ... Since the Spanish formatting has a comma, the SQL statement seems to have ...
    (microsoft.public.vb.general.discussion)
  • Re: Handling floating point with decimal comma separator to filter a form
    ... Lets say the user wants to filter values between 1,1 and 1,9 (note that the user wiil enter the number with a comma sparator, not with a dot - and the Standard format will be of no help here). ... Dim strWhere As String ...
    (microsoft.public.access.formscoding)
  • Re: How to convert International number strings to their number va
    ... format uses a comma, then the Number format should honor that. ... >>The JavaScript Numberfunction does not work correctly. ... >>convert a string that contains a comma to a number. ...
    (microsoft.public.scripting.jscript)
  • Re: SQL syntax error
    ... The regional setting is using comma instead of point (US decimal ... Aha, how should the string know that it is a currency value? ...
    (microsoft.public.access.formscoding)
  • Re: Cannot get format to work
    ... I need to display it in a Rich Text box using a comma separator for ... Is it just the format that's not right or is the value off by a small amount ... use something other than a comma for the thousands separator, ... The same goes for the currency symbol and the decimal symbol. ...
    (microsoft.public.vb.syntax)