Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > parseInt() question

Reply
Thread Tools

parseInt() question

 
 
kilik3000@gmail.com
Guest
Posts: n/a
 
      06-17-2007
Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn't you want to get back 18 in most if not all cases?

Any thoughts?

-Thx

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      06-17-2007
wrote on 17 jun 2007 in comp.lang.javascript:

> Why does parseInt("0000000000000018") return 1, while
> parseInt("0000000000000018", 10) return 18?
>
> My assumption was that the base 10 would be default argument for
> radix.


Your assumption is wrong, it is octal.
Read the specs:

parseInt(numString, [radix])

numString
Required. A string to convert into a number.

radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of '0x' are
considered hexadecimal and strings with a prefix of '0' are considered
octal. All other strings are considered decimal.



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      06-17-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Why does parseInt("0000000000000018") return 1, while
> parseInt("0000000000000018", 10) return 18?
>
> My assumption was that the base 10 would be default argument for
> radix. Wouldn't you want to get back 18 in most if not all cases?


There was a mistake made in the specification of parseInt. That is why you
should always explicitly indicate the radix. Don't depend on the default being
10. As you demonstrated, it is not reliable.

JSLint will read your source and identify the places where the default is missing.

http://www.JSLint.com/
 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      06-17-2007
Evertjan. wrote:
> wrote on 17 jun 2007 in comp.lang.javascript:
>
>> Why does parseInt("0000000000000018") return 1, while
>> parseInt("0000000000000018", 10) return 18?
>>
>> My assumption was that the base 10 would be default argument for
>> radix.

>
> Your assumption is wrong, it is octal.
> Read the specs:
>
> parseInt(numString, [radix])
>
> numString
> Required. A string to convert into a number.
>
> radix
> Optional. A value between 2 and 36 indicating the base of the number
> contained in numString. If not supplied, strings with a prefix of '0x' are
> considered hexadecimal and strings with a prefix of '0' are considered
> octal. All other strings are considered decimal.


I'd like to know where you read that from. The Core JavaScript 1.5
specifically states that, that behavior is deprecated.

http://developer.mozilla.org/en/docs...nt#Description

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-17-2007
-Lost wrote on 17 jun 2007 in comp.lang.javascript:

> Evertjan. wrote:


>> radix
>> Optional. A value between 2 and 36 indicating the base of the number
>> contained in numString. If not supplied, strings with a prefix of
>> '0x' are considered hexadecimal and strings with a prefix of '0' are
>> considered octal. All other strings are considered decimal.

>
> I'd like to know where you read that from.


MS

> The Core JavaScript 1.5
> specifically states that, that behavior is deprecated.


Could be that the behavior is deprecated,
but it still seems to work that way.

<script type='text/javascript'>
alert(parseInt('00018')) // returns 1 in IE7 and FF2
</script>

Do you sometimes feel deprecated and Lost forever too,
dreadful sorry, Clementine?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      06-17-2007
Evertjan. wrote:
> -Lost wrote on 17 jun 2007 in comp.lang.javascript:
>
>> Evertjan. wrote:

>
>>> radix
>>> Optional. A value between 2 and 36 indicating the base of the number
>>> contained in numString. If not supplied, strings with a prefix of
>>> '0x' are considered hexadecimal and strings with a prefix of '0' are
>>> considered octal. All other strings are considered decimal.

>> I'd like to know where you read that from.

>
> MS


Ah, OK.

>> The Core JavaScript 1.5
>> specifically states that, that behavior is deprecated.

>
> Could be that the behavior is deprecated,
> but it still seems to work that way.
>
> <script type='text/javascript'>
> alert(parseInt('00018')) // returns 1 in IE7 and FF2
> </script>


Right, I see that. Don't understand it, but I see it.

> Do you sometimes feel deprecated and Lost forever too,
> dreadful sorry, Clementine?


I am not sure I understand what you said, but yes, I am lost quite often
(front lobe disabilities affect problem solving). Anyway, I never fully
understand how parseInt works. I have to read it a thousand times
before realizing (for example) that:

parseInt('18' ; should *not* return 22, but parseInt('22', ; should
return 18.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-17-2007
-Lost wrote on 17 jun 2007 in comp.lang.javascript:

>> alert(parseInt('00018')) // returns 1 in IE7 and FF2

>
> Right, I see that. Don't understand it, but I see it.


I think [but am not sure] it works this way:

0 octal assumed

00 skipped

1 value is one

8 value over 7 not part of octal number,
so 8 is considered to be a letter,
parsing ended.

result value is 1.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      06-17-2007
Evertjan. said the following on 6/17/2007 2:10 PM:
> -Lost wrote on 17 jun 2007 in comp.lang.javascript:
>
>>> alert(parseInt('00018')) // returns 1 in IE7 and FF2

>> Right, I see that. Don't understand it, but I see it.

>
> I think [but am not sure] it works this way:
>
> 0 octal assumed
>
> 00 skipped
>
> 1 value is one
>
> 8 value over 7 not part of octal number,
> so 8 is considered to be a letter,
> parsing ended.
>
> result value is 1.
>


Read the string, from left to right, until you encounter a character
that is not in the base set. The string that you have read, up until
then, convert it to the base. So, it reads until it finds the 8, stops
reading (parseInt('000181111') will also - rightfully - give 1). Then it
converts 0001 in Base 8, which is 1.

parseInt('0001238') might show it a little easier to see.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-18-2007
In comp.lang.javascript message <(E-Mail Removed)
oglegroups.com>, Sun, 17 Jun 2007 09:55:37, (E-Mail Removed) posted:
>Why does parseInt("0000000000000018") return 1, while
>parseInt("0000000000000018", 10) return 18?
>
>My assumption was that the base 10 would be default argument for
>radix. Wouldn't you want to get back 18 in most if not all cases?
>
>Any thoughts?


You should have read the newsgroup FAQ. One section fairly obviously
applies. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-18-2007
In comp.lang.javascript message <E9CdnZGwH5q57ejbnZ2dnUVZ_tKjnZ2d@comcas
t.com>, Sun, 17 Jun 2007 13:33:54, -Lost <(E-Mail Removed)>
posted:
>
>I'd like to know where you read that from. The Core JavaScript 1.5
>specifically states that, that behavior is deprecated.


If something is currently deprecated, it can be assumed to exist.

However, it should not be used if there is a non-deprecated alternative,
and it should not be relied upon.

OTOH, when code is being read, it is well to be able to understand the
deprecated construct.

The FAQ refers.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
 
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
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments