Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How convert string '1e7' to an integer?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Peng Yu  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:17
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Sat, 7 Nov 2009 19:17:09 -0600
Local: Sun 8 Nov 2009 01:17
Subject: 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'

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mensanator  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:41
Newsgroups: comp.lang.python
From: Mensanator <mensana...@aol.com>
Date: Sat, 7 Nov 2009 17:41:03 -0800 (PST)
Local: Sun 8 Nov 2009 01:41
Subject: Re: How convert string '1e7' to an integer?
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mick Krippendorf  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:42
Newsgroups: comp.lang.python
From: Mick Krippendorf <mad.m...@gmx.de>
Date: Sun, 08 Nov 2009 02:42:21 +0100
Local: Sun 8 Nov 2009 01:42
Subject: Re: How convert string '1e7' to an integer?
Peng Yu wrote:
> It seems that int() does not convert '1e7'.

It seems it does, though:

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

487

Mick.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:45
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Sun, 08 Nov 2009 01:45:57 +0000
Local: Sun 8 Nov 2009 01:45
Subject: Re: How convert string '1e7' to an integer?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:49
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Sun, 08 Nov 2009 12:49:48 +1100
Local: Sun 8 Nov 2009 01:49
Subject: Re: How convert string '1e7' to an integer?

Mick Krippendorf <mad.m...@gmx.de> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gary Herron  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:50
Newsgroups: comp.lang.python
From: Gary Herron <gher...@islandtraining.com>
Date: Sat, 07 Nov 2009 17:50:53 -0800
Local: Sun 8 Nov 2009 01:50
Subject: Re: How convert string '1e7' to an integer?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:52
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Sat, 7 Nov 2009 20:52:37 -0500
Local: Sun 8 Nov 2009 01:52
Subject: Re: How convert string '1e7' to an integer?

On Sat, Nov 7, 2009 at 8:17 PM, Peng Yu <pengyu...@gmail.com> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Heimes  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 01:55
Newsgroups: comp.lang.python
From: Christian Heimes <li...@cheimes.de>
Date: Sun, 08 Nov 2009 02:55:08 +0100
Local: Sun 8 Nov 2009 01:55
Subject: Re: How convert string '1e7' to an integer?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 02:05
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Sat, 07 Nov 2009 20:05:22 -0600
Local: Sun 8 Nov 2009 02:05
Subject: Re: How convert string '1e7' to an integer?

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roel Schroeven  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 09:37
Newsgroups: comp.lang.python
From: Roel Schroeven <rschroev_nospam...@fastmail.fm>
Date: Sun, 08 Nov 2009 10:37:34 +0100
Local: Sun 8 Nov 2009 09:37
Subject: Re: How convert string '1e7' to an integer?
Gary Herron schreef:

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:40
Newsgroups: comp.lang.python
From: Thomas <thom1...@gmail.com>
Date: Sun, 8 Nov 2009 04:40:29 -0800 (PST)
Local: Sun 8 Nov 2009 12:40
Subject: Re: How convert string '1e7' to an integer?
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>


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mick Krippendorf  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 13:39
Newsgroups: comp.lang.python
From: Mick Krippendorf <mad.m...@gmx.de>
Date: Sun, 08 Nov 2009 14:39:05 +0100
Local: Sun 8 Nov 2009 13:39
Subject: Re: How convert string '1e7' to an integer?
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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google