Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How convert string '1e7' to an integer?

Reply
Thread Tools

How convert string '1e7' to an integer?

 
 
Peng Yu
Guest
Posts: n/a
 
      11-08-2009
It seems that int() does not convert '1e7'. I'm wondering what
function to use to convert '1e7' to an integer?

>>> int('1e7')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1e7'
 
Reply With Quote
 
 
 
 
Mensanator
Guest
Posts: n/a
 
      11-08-2009
On Nov 7, 7:17*pm, Peng Yu <(E-Mail Removed)> wrote:
> It seems that int() does not convert '1e7'.


Because 'e' isn't a valid character in base 10.

> 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'


 
Reply With Quote
 
 
 
 
Mick Krippendorf
Guest
Posts: n/a
 
      11-08-2009
Peng Yu wrote:
> It seems that int() does not convert '1e7'.

It seems it does, though:

>>> int('1e7', base=16)

487

Mick.
 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      11-08-2009
Mensanator wrote:
> On Nov 7, 7:17 pm, Peng Yu <(E-Mail Removed)> 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'
>>

>
>


 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      11-08-2009
Mick Krippendorf wrote:
> Peng Yu wrote:
>> It seems that int() does not convert '1e7'.

> It seems it does, though:
>
>>>> int('1e7', base=16)

> 487


Bah...so narrow-minded

>>> print '\n'.join("Base %i: %i" % (base, int('1e7',

base=base)) for base in range(15,37))
Base 15: 442
Base 16: 487
Base 17: 534
Base 18: 583
Base 19: 634
Base 20: 687
Base 21: 742
Base 22: 799
Base 23: 858
Base 24: 919
Base 25: 982
Base 26: 1047
Base 27: 1114
Base 28: 1183
Base 29: 1254
Base 30: 1327
Base 31: 1402
Base 32: 1479
Base 33: 1558
Base 34: 1639
Base 35: 1722
Base 36: 1807

I feel so dirty interpreting numbers in convenient ways...like an
accountant. ("whaddaya mean I can't file my tax-return in base
17?! There's nothing in the supporting documentation that
mentions such draconian restrictions!")

-tkc




 
Reply With Quote
 
Roel Schroeven
Guest
Posts: n/a
 
      11-08-2009
Gary Herron schreef:
> Mensanator wrote:
>> On Nov 7, 7:17 pm, Peng Yu <(E-Mail Removed)> 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


If that is a concern, decimal can help:

>>> import decimal
>>> int(decimal.Decimal('1e30'))

1000000000000000000000000000000L

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 
Reply With Quote
 
Thomas
Guest
Posts: n/a
 
      11-08-2009
Just a curiosity, why does Python do this?

>>> l = [(base, int('1e7', base=base)) for base in range(15,37)]
>>> l

[(15, 442), (16, 487), (17, 534), (18, 583), (19, 634), (20, 687),
(21, 742), (22, 799), (23, 85, (24, 919), (25, 982), (26, 1047),
(27, 1114), (28, 1183), (29, 1254), (30, 1327), (31, 1402), (32,
1479), (33, 155, (34, 1639), (35, 1722), (36, 1807)]
>>> l = ([base, int('1e7', base=base)] for base in range(15,37))
>>> l

<generator object at 0x027803A0>
>>>

 
Reply With Quote
 
Mick Krippendorf
Guest
Posts: n/a
 
      11-08-2009
Thomas wrote:
> Just a curiosity, why does Python do this?
>
>>>> [(base, int('1e7', base=base)) for base in range(15,37)]

> [(15, 442), (16, 487), (17, 534), (18, 583), (19, 634), (20, 687),
> (21, 742), (22, 799), (23, 85, (24, 919), (25, 982), (26, 1047),
> (27, 1114), (28, 1183), (29, 1254), (30, 1327), (31, 1402), (32,
> 1479), (33, 155, (34, 1639), (35, 1722), (36, 1807)]
>>>> ([base, int('1e7', base=base)] for base in range(15,37))

> <generator object at 0x027803A0>

Because the former is a list comprehension, whereas the latter is a
generator expression.

Mick.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Convert formatted string to string? rockdale ASP .Net 3 04-12-2006 03:01 PM
convert string to raw string? Phd Python 3 12-06-2004 04:36 PM
Connection String object Convert to String Variable Type =?Utf-8?B?TWlrZSBNb29yZQ==?= ASP .Net 2 10-26-2004 02:43 PM
Convert string to a string array Andrew Banks ASP .Net 2 04-19-2004 04:45 PM



Advertisments