atof problem.

richard_chamberlain richard_chamberlain at ntlworld.com
Fri Jul 14 17:05:59 EDT 2000


Hi William,

Yes you could use the locale module to do what you want:

import locale
s="123,456,789.2"
print locale.atof(s)

As Bjorn said you'll have to get the locale module from somewhere.

Otherwise do something like below:

import string
def rcatof(num):
    try:
        flt=float(string.replace(num,',',''))
        return flt
    except:
        return None

>>> import myScript
>>> myScript.rcatof('123,456.2')
123456.2

Richard





William Dandreta <wjdandreta at worldnet.att.net> wrote in message
news:yUqb5.977$tI4.52638 at bgtnsc05-news.ops.worldnet.att.net...
> I am having a problem with the atof function. I tried to convert 1,142.00
to
> a float but the comma causes a problem. I resolved the problem by removing
> the comma but there must be a better way.
>
> I looked in the documentation and there was mention of the $LANG
> environmental variable but it did not tell haw to use it. Also, it
mentioned
> settings could be made using the functions in locale module. I tried to
> import the locale module but I get an error message that the _locale
module
> doesn't exist.
>
> Anyone have a clue about this?
>
> Bill
>
>





More information about the Python-list mailing list