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

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

FAQ server
Guest
Posts: n/a

 12-19-2006
-----------------------------------------------------------------------
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
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.

Richard Cornford
Guest
Posts: n/a

 12-19-2006
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.

Randy Webb
Guest
Posts: n/a

 12-19-2006
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/

Julian Turner
Guest
Posts: n/a

 12-19-2006

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

Regards

Julian

Michael Winter
Guest
Posts: n/a

 12-19-2006
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

Randy Webb
Guest
Posts: n/a

 12-19-2006
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

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

Dr J R Stockton
Guest
Posts: n/a

 12-19-2006
In comp.lang.javascript message
<45872b86\$0\$49198\$>, Tue, 19 Dec 2006 00:00:02,
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.
>Assuming octal, the string '09' will be converted to 0 (octal
>digits are 0-7); assuming decimal, '09' will be converted to 9
> 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.

Randy Webb
Guest
Posts: n/a

 12-19-2006
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,
</new proposal>

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

Randy Webb
Guest
Posts: n/a

 12-20-2006
Dr J R Stockton said the following on 12/19/2006 5:18 PM:
> In comp.lang.javascript message
> <45872b86\$0\$49198\$>, Tue, 19 Dec 2006 00:00:02,
> 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.
>> 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/

Dr J R Stockton
Guest
Posts: n/a

 12-20-2006
In comp.lang.javascript message <>,
Tue, 19 Dec 2006 21:17:17, Randy Webb <> wrote:
>Dr J R Stockton said the following on 12/19/2006 5:18 PM:
>> In comp.lang.javascript message
>> <45872b86\$0\$49198\$>, Tue, 19 Dec 2006 00:00:02,
>> 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.
>>> 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

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