How convert string '1e7' to an integer?

Gary Herron gherron at islandtraining.com
Sat Nov 7 20:50:53 EST 2009


Mensanator wrote:
> On Nov 7, 7:17 pm, Peng Yu <pengyu... at gmail.com> wrote:
>   
>> It seems that int() does not convert '1e7'.
>>     
>
> Because 'e' isn't a valid character in base 10.
>   

But 1e7 is a valid float, so this works:

 >>> int(float('1e7'))
10000000

That has a problem though, if you surpass the ability of a float:

 >>> int(float('1e20'))
100000000000000000000L
 >>> int(float('1e30'))
1000000000000000019884624838656L


Gary Herron



>   
>> I'm wondering what
>> function to use to convert '1e7' to an integer?
>>
>>     
>>>>> int('1e7')
>>>>>           
>
>   
>>>> int(1e7)
>>>>         
> 10000000
>
>
>   
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> ValueError: invalid literal for int() with base 10: '1e7'
>>     
>
>   




More information about the Python-list mailing list