By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

FAQ Topic - Why does K = parseInt('09') set K to 0?

P: n/a
-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
parseInt("09",base)

http://msdn.microsoft.com/library/en...thparseint.asp

http://docs.sun.com/source/816-6408-...ev.htm#1064173

http://www.jibbering.com/faq/faq_not....html#FAQN4_12
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Dec 19 '06 #1
Share this Question
Share on Google+
14 Replies

P: n/a
FAQ server wrote:
-------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
<snip>

Shouldn't at least the second and third occurrence of the word "number"
in those sentences be "string", and possible all of the first three?

The addition of an opening sentence stating that "the parseInt function
takes sting input and parses it into numeric output" (or something like
that) may make the following text easier to render accurate without its
becoming obscure in the process.

Richard.
Dec 19 '06 #2

P: n/a
Richard Cornford said the following on 12/18/2006 7:50 PM:
FAQ server wrote:
>-------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
<snip>

Shouldn't at least the second and third occurrence of the word "number"
in those sentences be "string", and possible all of the first three?
Probably all three.
The addition of an opening sentence stating that "the parseInt function
takes sting input and parses it into numeric output" (or something like
that) may make the following text easier to render accurate without its
becoming obscure in the process.
<proposal>
The parseInt function decides what base to convert a string
to by looking at the string. It assumes that any string beginning
with '0x' or '0X' represents a hexadecimal Number, but it has a
choice with a leading zero: the string can represent a Number that
can be either octal or decimal. Assuming octal, the string '09'
will be converted to 0 (octal digits are 0-7); assuming decimal,
'09' will be converted to 9 (the leading zero is ignored). To force
use of a particular base, add a second parameter: parseInt("09",base).
</proposal>

Sidenote: The MSDN URL for that article has been changed by MS. I guess
I need to double check all of the MSDN URL's to make sure the links in
the FAQ don't lead to a redirect page.

Any idea why http://jibbering.com/faq/ gives the old version instead of
the updated version? At least for me it does.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 19 '06 #3

P: n/a

Randy Webb wrote:

Hi

May I hazard some amateur comments:-

[snip]
<proposal>
The parseInt function decides what base to convert a string
to by looking at the string.
Perhaps "...to convert a number represented in a string by...."
>It assumes that any string beginning
with '0x' or '0X' represents a hexadecimal Number,
Should "Number" be in upper case? Might that confuse numeric literals,
with JavaScript's internal Number data type?

Perhaps "...indicates a number represented in hexadecimal..."
>but it has a
"...but, if an 'x' or 'X' is not present, has a..."
choice with a leading zero: the string can represent a Number that
can be either octal or decimal.
"...could represent a number...."
>Assuming octal, the string '09'
"A leading '0' will cause octal will be the first assumption, otherwise
decimal is assumed. As a result the string '09'...."
will be converted to 0 (octal digits are 0-7);
"....(valid octal digits are 0-7, so parseInt will stop parsing when it
reaches '9')..."
>assuming decimal, '09' will be converted to 9 (the leading zero is ignored). To force
use of a particular base, add a second parameter: parseInt("09",base).
</proposal>
"To force use of a particular base, add a second parameter:
parseInt("09",base). If decimal is forced, '09' will be converted to 9
(the leading zero is ignored)."

Regards

Julian

Dec 19 '06 #4

P: n/a
Randy Webb wrote:
Richard Cornford said the following on 12/18/2006 7:50 PM:
>FAQ server wrote:
>>-------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
<snip>

Shouldn't at least the second and third occurrence of the word "number"
in those sentences be "string", and possible all of the first three?
The second, yes. The first and third might be reworded, but to replace
either with "string" is incorrect. In both cases, it is the number
represented within the string that's significant, something that I
clearly failed to communicate.

A string doesn't have to begin with '0x' or '0X' to be treated as
hexadecimal; it does have '0x' or '0X' as the first non-white space
characters.

[snip]
Any idea why http://jibbering.com/faq/ gives the old version instead of
the updated version? At least for me it does.
No, it's bizarre. Both /faq/index.html and /faq/index.asp lead to the
updated version but none of the other typical directory index paths that
I can think of refer to either FAQ version. Perhaps Jim or his host can
shed some light on it.

The FAQ is currently sent with a "Cache-Control: private" header
name/value pair and no cache validators. It would nice if this were
changed so visitors don't have to download the FAQ again unless it's
been modified. The private Cache-Control directive doesn't necessarily
restrict a user agent cache, but does unnecessarily prevent shared
caches from storing the entity.

Mike
Dec 19 '06 #5

P: n/a
Michael Winter said the following on 12/19/2006 3:44 PM:
Randy Webb wrote:
>Richard Cornford said the following on 12/18/2006 7:50 PM:
>>FAQ server wrote:
-------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
<snip>

Shouldn't at least the second and third occurrence of the word "number"
in those sentences be "string", and possible all of the first three?

The second, yes. The first and third might be reworded, but to replace
either with "string" is incorrect. In both cases, it is the number
represented within the string that's significant, something that I
clearly failed to communicate.

A string doesn't have to begin with '0x' or '0X' to be treated as
hexadecimal; it does have '0x' or '0X' as the first non-white space
characters.
What do you think about the proposed re-wording?
[snip]
>Any idea why http://jibbering.com/faq/ gives the old version instead
of the updated version? At least for me it does.

No, it's bizarre. Both /faq/index.html and /faq/index.asp lead to the
updated version but none of the other typical directory index paths that
I can think of refer to either FAQ version. Perhaps Jim or his host can
shed some light on it.
One very ironic thing about it is that I get the new faq.css file even
with 8.1
The FAQ is currently sent with a "Cache-Control: private" header
name/value pair and no cache validators. It would nice if this were
changed so visitors don't have to download the FAQ again unless it's
been modified. The private Cache-Control directive doesn't necessarily
restrict a user agent cache, but does unnecessarily prevent shared
caches from storing the entity.
I will leave that to Jim to change/correct as it is outside the bounds
of my access to the server.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 19 '06 #6

P: n/a
Julian Turner said the following on 12/19/2006 6:07 AM:
Randy Webb wrote:

Hi

May I hazard some amateur comments:-

[snip]
><proposal>
The parseInt function decides what base to convert a string
to by looking at the string.

Perhaps "...to convert a number represented in a string by...."
Changed.
>It assumes that any string beginning
with '0x' or '0X' represents a hexadecimal Number,

Should "Number" be in upper case? Might that confuse numeric literals,
with JavaScript's internal Number data type?
Changed to lower case. Not sure why I made it Number instead of number.
Perhaps "...indicates a number represented in hexadecimal..."
>but it has a

"...but, if an 'x' or 'X' is not present, has a..."
>choice with a leading zero: the string can represent a Number that
can be either octal or decimal.

"...could represent a number...."
Changed "can" to "could".
>Assuming octal, the string '09'

"A leading '0' will cause octal will be the first assumption, otherwise
decimal is assumed. As a result the string '09'...."
I think that goes without saying.
>will be converted to 0 (octal digits are 0-7);

"....(valid octal digits are 0-7, so parseInt will stop parsing when it
reaches '9')..."
I don't even think the (octal..) should be there.
>assuming decimal, '09' will be converted to 9 (the leading zero is ignored). To force
use of a particular base, add a second parameter: parseInt("09",base).
</proposal>

"To force use of a particular base, add a second parameter:
parseInt("09",base). If decimal is forced, '09' will be converted to 9
(the leading zero is ignored)."
Wouldn't that be obvious without saying it?

<new proposal>
The parseInt function decides what base to
convert a number represented as a string
to by looking at the string. It assumes that
any string beginning with '0x' or '0X' represents
a hexadecimal number, but it has a choice with a
leading zero: the string could represent a number
that can be either octal or decimal. Assuming
octal, the string '09' will be converted to 0
(octal digits are 0-7); assuming decimal, '09'
will be converted to 9 (the leading zero is
ignored). To force use of a particular base,
add a second parameter: parseInt("09",base).
</new proposal>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 19 '06 #7

P: n/a
In comp.lang.javascript message
<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,
FAQ server <ja********@dotinternet.bewrote:
>-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
‹ parseInt("09",base) ›
The text does not really answer the Subject question.

Function parseInt has two important features that other conversions lack
: it has flexibility of base, AND, after the first digit, it treats the
first character that is not a digit in the current base as a legitimate
terminator (that may not be 100% accurate, but indicates the point).
Contrast parseInt("6px") with Number("6px") and +"6px" .
So put "Function parseInt reads a String and returns a corresponding
Number; conversion of the digital part ends at any character which is
not a digit in the current base. If parseInt('09') is considered
octal, the base is 8, the 9 ends conversion, and Number 0 is returned.
So parseInt('0449') would then return Number octal 44 = decimal 36."

then more or less as before with 'string' wherever the input is being
referred to.
In parseInt(S, B) there remains the question of how B is handled : in
IE6, parseInt("0449", 012) gives Number 449. In IE6, it seems that
literals are treated as if by parseInt(S), with 012 being ten (ISTM
that IE4 maybe took it as 12). That can be important in date work,
where one may well be tempted to write
Oct7th = new Date(Y, 09, 07)
Nov7th = new Date(Y, 10, 07)
for alignment, and I think FF2 may object to that. Then in dealing with
Ordinal Date YYYY-DDD in IE6 a literal 044 is thirty-six but 088 is
eighty-eight.
So I suggest a final paragraph to that FAQ section -
"A literal L in the source is read as if by parseInt(L), so 012 means
ten." - or whatever similar covers all cases.

A better Subject might be "Why is my 09 read as Zero?" - it accommodates
more.
Where a RegExp is used to split a string into decimal parts, ISTM that
there could be advantage in expressing a part not as (\d+) but as
0*(\d+) thereby excluding leading zeroes. I've not yet seen a need to
do so, but ... .

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Dec 19 '06 #8

P: n/a
Dr J R Stockton said the following on 12/19/2006 5:18 PM:
In comp.lang.javascript message
<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,
FAQ server <ja********@dotinternet.bewrote:
>-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
parseInt("09",base)

The text does not really answer the Subject question.
Why not? It explains how parseInt deals with the first parameter, which
is what the question was intended to answer.
Function parseInt has two important features that other conversions lack
: it has flexibility of base, AND, after the first digit, it treats the
first character that is not a digit in the current base as a legitimate
terminator (that may not be 100% accurate, but indicates the point).
Contrast parseInt("6px") with Number("6px") and +"6px" .

So put "Function parseInt reads a String and returns a corresponding
Number; conversion of the digital part ends at any character which is
not a digit in the current base. If parseInt('09') is considered
octal, the base is 8, the 9 ends conversion, and Number 0 is returned.
So parseInt('0449') would then return Number octal 44 = decimal 36."
That is something that goes way beyond the FAQ question and should be in
a Notes page (or elsewhere) and linked to.
then more or less as before with 'string' wherever the input is being
referred to.
In parseInt(S, B) there remains the question of how B is handled : in
IE6, parseInt("0449", 012) gives Number 449. In IE6, it seems that
literals are treated as if by parseInt(S), with 012 being ten (ISTM
that IE4 maybe took it as 12).
IE7 gives the same results as IE6 with parseInt("0449", 012).
That can be important in date work,
where one may well be tempted to write
Oct7th = new Date(Y, 09, 07)
Nov7th = new Date(Y, 10, 07)
for alignment, and I think FF2 may object to that. Then in dealing with
Ordinal Date YYYY-DDD in IE6 a literal 044 is thirty-six but 088 is
eighty-eight.
FF2 gives the same results for Oct7th and Nov7th as IE7 does so it
doesn't object to it. Not sure why but it doesn't.

As for alignment, I would write it as such:

Oct7th = new Date(Y, 9, 07)
Nov7th = new Date(Y, 10, 07)

But that issue seems to be Date related, not parseInt related or does
the Date Object use parseInt to read its parameters?
So I suggest a final paragraph to that FAQ section -
"A literal L in the source is read as if by parseInt(L), so 012 means
ten." - or whatever similar covers all cases.
"The same rules apply to the Base parameter as apply to the first
parameter" or similar?
A better Subject might be "Why is my 09 read as Zero?" - it accommodates
more.
But it doesn't cover - directly - the issue with using parseInt on "09"
or the likes.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 20 '06 #9

P: n/a
In comp.lang.javascript message <rt********************@telcove.net>,
Tue, 19 Dec 2006 21:17:17, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/19/2006 5:18 PM:
>In comp.lang.javascript message
<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,
FAQ server <ja********@dotinternet.bewrote:
>>-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
‹ parseInt("09",base) ›
The text does not really answer the Subject question.

Why not? It explains how parseInt deals with the first parameter, which
is what the question was intended to answer.
While your responses are correctly interleaved with what you quote, it
is an error to immediately question a paragraph which is explained and
justified in the following quoted paragraph. Before beginning an
answer, one should read and understand (as far as possible) the whole of
the previous article.
>Function parseInt has two important features that other conversions lack
: it has flexibility of base, AND, after the first digit, it treats the
first character that is not a digit in the current base as a legitimate
terminator (that may not be 100% accurate, but indicates the point).
Contrast parseInt("6px") with Number("6px") and +"6px" .
So put "Function parseInt reads a String and returns a
corresponding
Number; conversion of the digital part ends at any character which is
not a digit in the current base. If parseInt('09') is considered
octal, the base is 8, the 9 ends conversion, and Number 0 is returned.
So parseInt('0449') would then return Number octal 44 = decimal 36."

That is something that goes way beyond the FAQ question and should be
in a Notes page (or elsewhere) and linked to.
>then more or less as before with 'string' wherever the input is being
referred to.
In parseInt(S, B) there remains the question of how B is handled
: in
IE6, parseInt("0449", 012) gives Number 449. In IE6, it seems that
literals are treated as if by parseInt(S), with 012 being ten (ISTM
that IE4 maybe took it as 12).

IE7 gives the same results as IE6 with parseInt("0449", 012).
>That can be important in date work,
where one may well be tempted to write
Oct7th = new Date(Y, 09, 07)
Nov7th = new Date(Y, 10, 07)
for alignment, and I think FF2 may object to that. Then in dealing with
Ordinal Date YYYY-DDD in IE6 a literal 044 is thirty-six but 088 is
eighty-eight.

FF2 gives the same results for Oct7th and Nov7th as IE7 does so it
doesn't object to it. Not sure why but it doesn't.

As for alignment, I would write it as such:

Oct7th = new Date(Y, 9, 07)
Nov7th = new Date(Y, 10, 07)
Indeed; but the existence of an alternative does not prevent the
question being raised. Since in most IT using numeric dates leading
zeroes are used, there is a definite temptation to write the month 9 as
"09".
>But that issue seems to be Date related, not parseInt related or does
the Date Object use parseInt to read its parameters?
>So I suggest a final paragraph to that FAQ section -
"A literal L in the source is read as if by parseInt(L), so 012 means
ten." - or whatever similar covers all cases.

"The same rules apply to the Base parameter as apply to the first
parameter" or similar?
NO. ICBW, but ISTM that a literal base parameter is treated as any
other literal. Therefore, it would be wrong to make a remark
specifically about the base parameter.

>A better Subject might be "Why is my 09 read as Zero?" - it accommodates
more.

But it doesn't cover - directly - the issue with using parseInt on "09"
or the likes.

Indeed it does cover it; though it does not cover 012 being read as ten.
Why answer a narrow question when a broader one would, without needing
to say much more, deal with the full range of similar cases?

Subject "Why are Strings beginning with a zero digit sometimes read as
unexpected integer Numbers?"

--
(c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Dec 20 '06 #10

P: n/a
Dr J R Stockton said the following on 12/20/2006 6:40 PM:
In comp.lang.javascript message <rt********************@telcove.net>,
Tue, 19 Dec 2006 21:17:17, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/19/2006 5:18 PM:
>>In comp.lang.javascript message
<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,
FAQ server <ja********@dotinternet.bewrote:
-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------

The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
parseInt("09",base)
The text does not really answer the Subject question.

Why not? It explains how parseInt deals with the first parameter,
which is what the question was intended to answer.

While your responses are correctly interleaved with what you quote, it
is an error to immediately question a paragraph which is explained and
justified in the following quoted paragraph.
No, the error is to think you use common sense when you post.

I am beginning to realize why Richard ignored so many of your FAQ
Requests. The current wording of that question was a result of a direct
request by you to change it to it's current wording and now you are
saying it is wrong. Make up your mind.

<snip>
>But that issue seems to be Date related, not parseInt related or does
the Date Object use parseInt to read its parameters?
>>So I suggest a final paragraph to that FAQ section -
"A literal L in the source is read as if by parseInt(L), so 012 means
ten." - or whatever similar covers all cases.

"The same rules apply to the Base parameter as apply to the first
parameter" or similar?

NO. ICBW, but ISTM that a literal base parameter is treated as any
other literal. Therefore, it would be wrong to make a remark
specifically about the base parameter.
Then there will be no remark at all.
>>A better Subject might be "Why is my 09 read as Zero?" - it accommodates
more.

But it doesn't cover - directly - the issue with using parseInt on
"09" or the likes.


Indeed it does cover it; though it does not cover 012 being read as ten.
Why answer a narrow question when a broader one would, without needing
to say much more, deal with the full range of similar cases?

Subject "Why are Strings beginning with a zero digit sometimes read as
unexpected integer Numbers?"
Do you honestly believe that people looking for an answer to
parseInt('09') being set to zero are going to look at a question phrased
as you have it there? The FAQ is intended - or it should be - for people
trying to learn from it, not from Anally Retentive know-it-alls that
want a quick personal reference document.

Unless there is some other problem with that entry (from someone besides
you), it will stay worded the way it is - along with the question.

I am not going to argue pedantic BS with you.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 21 '06 #11

P: n/a
In comp.lang.javascript message <Co********************@telcove.net>,
Thu, 21 Dec 2006 10:32:41, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/20/2006 6:40 PM:
>In comp.lang.javascript message <rt********************@telcove.net>,
Tue, 19 Dec 2006 21:17:17, Randy Webb <Hi************@aol.comwrote:
>>Dr J R Stockton said the following on 12/19/2006 5:18 PM:
In comp.lang.javascript message
<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,
FAQ server <ja********@dotinternet.bewrote:
-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------
>
The parseInt function decides what base the number is by
looking at the number. It assumes that any number beginning
with '0x' or '0X' is hexadecimal, but it has a choice with a
leading zero: the number can either be octal or decimal.
Assuming octal, the string '09' will be converted to 0 (octal
digits are 0-7); assuming decimal, '09' will be converted to 9
(the leading zero is ignored).
To force use of a particular base, add a second parameter:
‹ parseInt("09",base) ›
The text does not really answer the Subject question.

Why not? It explains how parseInt deals with the first parameter,
which is what the question was intended to answer.
While your responses are correctly interleaved with what you quote,
it is an error to immediately question a paragraph which is explained
and justified in the following quoted paragraph.

No, the error is to think you use common sense when you post.

I am beginning to realize why Richard ignored so many of your FAQ
Requests. The current wording of that question was a result of a direct
request by you to change it to it's current wording and now you are
saying it is wrong. Make up your mind.
Richard ignored all FAQ requests, independently of perceived merit. Is
that better or worse than lacking the ability to perceive merit?
><snip>
>>>So I suggest a final paragraph to that FAQ section -
"A literal L in the source is read as if by parseInt(L), so 012 means
ten." - or whatever similar covers all cases.

"The same rules apply to the Base parameter as apply to the first
parameter" or similar?
NO. ICBW, but ISTM that a literal base parameter is treated as any
other literal. Therefore, it would be wrong to make a remark
specifically about the base parameter.

Then there will be no remark at all.
>>>A better Subject might be "Why is my 09 read as Zero?" - it accommodates
more.

But it doesn't cover - directly - the issue with using parseInt on
"09" or the likes.
Indeed it does cover it; though it does not cover 012 being read as
ten. Why answer a narrow question when a broader one would, without
needing to say much more, deal with the full range of similar cases?
> Subject "Why are Strings beginning with a zero digit sometimes read
as unexpected integer Numbers?"

Do you honestly believe that people looking for an answer to
parseInt('09') being set to zero are going to look at a question
phrased as you have it there?
That Subject covers their problem; they have a string beginning with
zero, and they get unexpected answers. The present Subject line,
admittedly an improvement on its predecessor, will not appear to be
relevant to someone who finds that parseInt('044') gives thirty-six,
nor to someone who is using 012 as a literal and does not want ten.

>I am not going to argue pedantic BS with you.
You are unable to accept correction, and persistently argue with those
of broader experience than yourself. Therefore, you will be unable to
honour that promise.

--
(c) John Stockton, Surrey, UK. ??*@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.

Food expiry ambiguities: <URL:http://www.merlyn.demon.co.uk/date2k-3.htm#Food>
Dec 21 '06 #12

P: n/a
Dr J R Stockton said the following on 12/21/2006 5:41 PM:
In comp.lang.javascript message <Co********************@telcove.net>,
Thu, 21 Dec 2006 10:32:41, Randy Webb <Hi************@aol.comwrote:
>Dr J R Stockton said the following on 12/20/2006 6:40 PM:
<snip>
>I am beginning to realize why Richard ignored so many of your FAQ
Requests. The current wording of that question was a result of a
direct request by you to change it to it's current wording and now you
are saying it is wrong. Make up your mind.

Richard ignored all FAQ requests, independently of perceived merit. Is
that better or worse than lacking the ability to perceive merit?
Without confirmation from Richard that he did indeed ignore them rather
than read them and dismiss them, I don't see how you can make the claim
in your first sentence and that makes your question moot to me.

<snip>
>Do you honestly believe that people looking for an answer to
parseInt('09') being set to zero are going to look at a question
phrased as you have it there?

That Subject covers their problem; they have a string beginning with
zero, and they get unexpected answers. The present Subject line,
admittedly an improvement on its predecessor, will not appear to be
relevant to someone who finds that parseInt('044') gives thirty-six,
nor to someone who is using 012 as a literal and does not want ten.
And when that happens, they ask and get directed to that entry. They
read it and see the answer to the question. The solution is quite
simple, and you have argued with me in the past about how simple it is -
always supply a radix/base to parseInt and you won't have a problem.
>
>I am not going to argue pedantic BS with you.

You are unable to accept correction,
That is, without a doubt, one of the dumbest things I have ever read
that you wrote.
and persistently argue with those of broader experience than yourself.
If the experience you are referring to is in the area of being a
pedantically anal retentive poster, you are right, you have a lot
broader experience at than I do.

<snip>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 22 '06 #13

P: n/a
Randy Webb wrote:
Michael Winter said the following on 12/19/2006 3:44 PM:
[snip]
>A string doesn't have to begin with '0x' or '0X' to be treated as
hexadecimal; it does have '0x' or '0X' as the first non-white space
characters.

What do you think about the proposed re-wording?
That you posted in reply to Julian? It's OK. The last point I noted
above is difficult to convey succinctly, and I'm not sure how to write
that part well.

[snip]
>The FAQ is currently sent with a "Cache-Control: private" header
name/value pair and no cache validators. It would nice if this were
changed so visitors don't have to download the FAQ again unless
it's been modified. The private Cache-Control directive doesn't
necessarily restrict a user agent cache, but does unnecessarily
prevent shared caches from storing the entity.

I will leave that to Jim to change/correct as it is outside the
bounds of my access to the server.
And it has been. Thank you both.

Mike
Dec 28 '06 #14

P: n/a
Michael Winter said the following on 12/28/2006 11:17 AM:
Randy Webb wrote:
>Michael Winter said the following on 12/19/2006 3:44 PM:

[snip]
>>A string doesn't have to begin with '0x' or '0X' to be treated as
hexadecimal; it does have '0x' or '0X' as the first non-white space
characters.

What do you think about the proposed re-wording?

That you posted in reply to Julian? It's OK. The last point I noted
above is difficult to convey succinctly, and I'm not sure how to write
that part well.
I have changed it to the proposed posting in my reply to Julian.

<new wording locally>
The parseInt function decides what base to
convert a number represented as a string
to by looking at the string. It assumes that
any string beginning with '0x' or '0X' represents
a hexadecimal number, but it has a choice with a
leading zero: the string could represent a number
that can be either octal or decimal. Assuming
octal, the string '09' will be converted to 0
(octal digits are 0-7); assuming decimal, '09'
will be converted to 9 (the leading zero is
ignored). To force use of a particular base,
add a second parameter:
parseInt("09",base).
</new wording locally>

I probably won't upload another 9.xx series of the FAQ but it will be in
the proposed 10.x version. I will try, after 10.x gets finished, to
get a new wording for it that will reflect the leading whitespace issue
(probably make it a Notes page).

Thank you.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 30 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.