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

F

FAQ server

-----------------------------------------------------------------------
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-us/script56/html/js56jsmthparseint.asp

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

http://www.jibbering.com/faq/faq_notes/faq_notes.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.
 
R

Richard Cornford

FAQ said:
-------------------------------------------------------
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.
 
R

Randy Webb

Richard Cornford said the following on 12/18/2006 7:50 PM:
<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.
 
J

Julian Turner

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
 
M

Michael Winter

Randy said:
Richard Cornford said the following on 12/18/2006 7:50 PM:

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
 
R

Randy Webb

Michael Winter said the following on 12/19/2006 3:44 PM:
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.
 
D

Dr J R Stockton

In comp.lang.javascript message
-----------------------------------------------------------------------
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 ... .
 
R

Randy Webb

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, if an 'x' or 'X' is not present, has a..."

"...could represent a number...."

Changed "can" to "could".
"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.
"....(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.
"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>
 
R

Randy Webb

Dr J R Stockton said the following on 12/19/2006 5:18 PM:
In comp.lang.javascript message


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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Dr J R Stockton said the following on 12/19/2006 5:18 PM:

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.
That is something that goes way beyond the FAQ question and should be
in a Notes page (or elsewhere) and linked to.


IE7 gives the same results as IE6 with parseInt("0449", 012).


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?


"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.

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?"
 
R

Randy Webb

Dr J R Stockton said the following on 12/20/2006 6:40 PM:
In comp.lang.javascript message <[email protected]>,


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.

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.
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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Dr J R Stockton said the following on 12/20/2006 6:40 PM:

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>

Then there will be no remark at all.



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.
 
R

Randy Webb

Dr J R Stockton said the following on 12/21/2006 5:41 PM:
In comp.lang.javascript message <[email protected]>,



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.

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.
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>
 
M

Michael Winter

Randy said:
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]
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
 
R

Randy Webb

Michael Winter said the following on 12/28/2006 11:17 AM:
Randy said:
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.
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top