Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How convert string '1e7' to an integer?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 7, 2009, 5:17 PM

Post #1 of 12 (577 views)
Permalink
How convert string '1e7' to an integer?

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'
--
http://mail.python.org/mailman/listinfo/python-list


mensanator at aol

Nov 7, 2009, 5:41 PM

Post #2 of 12 (561 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

On Nov 7, 7:17 pm, Peng Yu <pengyu...@gmail.com> 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'

--
http://mail.python.org/mailman/listinfo/python-list


mad.mick at gmx

Nov 7, 2009, 5:42 PM

Post #3 of 12 (560 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Peng Yu wrote:
> It seems that int() does not convert '1e7'.
It seems it does, though:

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

Mick.
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 7, 2009, 5:45 PM

Post #4 of 12 (562 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Peng Yu wrote:
> 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'

In Python the e-form indicates a float, as does the presence of a
decimal point, but you can convert to float and then to int:

>>> int(float('1e7'))
10000000
--
http://mail.python.org/mailman/listinfo/python-list


ben+python at benfinney

Nov 7, 2009, 5:49 PM

Post #5 of 12 (562 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Mick Krippendorf <mad.mick [at] gmx> writes:

> Peng Yu wrote:
> > It seems that int() does not convert '1e7'.
> It seems it does, though:
>
> >>> int('1e7', base=16)
> 487

Well played, sir.

--
\ ā€œIt is wrong to think that the task of physics is to find out |
`\ how nature *is*. Physics concerns what we can *say* about |
_o__) natureā€¦ā€ —Niels Bohr |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


gherron at islandtraining

Nov 7, 2009, 5:50 PM

Post #6 of 12 (560 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Mensanator wrote:
> On Nov 7, 7:17 pm, Peng Yu <pengyu...@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'
>>
>
>

--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Nov 7, 2009, 5:52 PM

Post #7 of 12 (560 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

On Sat, Nov 7, 2009 at 8:17 PM, Peng Yu <pengyu.ut [at] gmail> wrote:
> 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'

Whenever you use that notation, you always get a float

>>> 1e7
10000000.0

So to convert '1e7' to a number, you need to use float('1e7') which
you can then convert to an int
>>> int(float('1e7'))
10000000



> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


lists at cheimes

Nov 7, 2009, 5:55 PM

Post #8 of 12 (561 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Peng Yu wrote:
> It seems that int() does not convert '1e7'. I'm wondering what
> function to use to convert '1e7' to an integer?

1e7 is a way to express a float in science and math. Try float("1e7")

Christian

--
http://mail.python.org/mailman/listinfo/python-list


python.list at tim

Nov 7, 2009, 6:05 PM

Post #9 of 12 (560 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

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




--
http://mail.python.org/mailman/listinfo/python-list


rschroev_nospam_ml at fastmail

Nov 8, 2009, 1:37 AM

Post #10 of 12 (544 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

Gary Herron schreef:
> Mensanator wrote:
>> On Nov 7, 7:17 pm, Peng Yu <pengyu...@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

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
--
http://mail.python.org/mailman/listinfo/python-list


thom1948 at gmail

Nov 8, 2009, 4:40 AM

Post #11 of 12 (540 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

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
<generator object at 0x027803A0>
>>>
--
http://mail.python.org/mailman/listinfo/python-list


mad.mick at gmx

Nov 8, 2009, 5:39 AM

Post #12 of 12 (541 views)
Permalink
Re: How convert string '1e7' to an integer? [In reply to]

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, 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.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.