How convert string '1e7' to an integer?

P

Peng Yu

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

Gary Herron

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

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

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


Gary Herron
 
T

Tim Chase

Mick said:
It seems it does, though:

487

Bah...so narrow-minded ;-)
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
 
R

Roel Schroeven

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

10000000

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

1000000000000000019884624838656L

If that is a concern, decimal can help:
1000000000000000000000000000000L

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

Roel Schroeven
 
T

Thomas

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, 858), (24, 919), (25, 982), (26, 1047),
(27, 1114), (28, 1183), (29, 1254), (30, 1327), (31, 1402), (32,
1479), (33, 1558), (34, 1639), (35, 1722), (36, 1807)]
l = ([base, int('1e7', base=base)] for base in range(15,37))
l
 
M

Mick Krippendorf

Thomas said:
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, 858), (24, 919), (25, 982), (26, 1047),
(27, 1114), (28, 1183), (29, 1254), (30, 1327), (31, 1402), (32,
1479), (33, 1558), (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top