Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 12th, 2008, 12:05 AM
FAQ server
Guest
 
Posts: n/a
Default FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

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

Method ` parseInt ` generally needs a
second parameter ` radix ` for the base.

Function ` parseInt ` decides what base to convert a
number represented as a string to by looking at the string. Any
string beginning with ` '0x' ` or ` '0X' ` represents a
hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
parameter: ` parseInt("09", base) `

http://msdn2.microsoft.com/en-us/library/x53yedee.aspx

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://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

  #2  
Old October 12th, 2008, 12:35 AM
Jorge
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 12, 1:00*am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------
>
Method ` parseInt ` generally needs a
second parameter ` radix ` for the base.
>
Function ` parseInt ` decides what base to convert a
number represented as a string to by looking at the string. Any
string beginning with ` '0x' ` or ` '0X' ` represents a
hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
parameter: ` parseInt("09", base) *`
>
http://msdn2.microsoft.com/en-us/library/x53yedee.aspx
>
http://docs.sun.com/source/816-6408-...ev.htm#1064173
>
http://www.jibbering.com/faq/faq_not....html#FAQN4_12
>

http://developer.mozilla.org/en/Core...tions/parseInt

or:
http://www.google.com/search?q=site:...a.org+parseint

or:
http://www.google.com/search?q=site:...l.org+parseint

--
Jorge.
  #3  
Old October 12th, 2008, 02:35 AM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

Jorge wrote:
Quote:
On Oct 12, 1:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
>-----------------------------------------------------------------------
>FAQ Topic - Why does K = parseInt('09') set K to 0?
>-----------------------------------------------------------------------
>>
>Method ` parseInt ` generally needs a
>second parameter ` radix ` for the base.
>>
>Function ` parseInt ` decides what base to convert a
parseInt does not make decisions.
Quote:
Quote:
>number represented as a string to by looking at the string. Any
and it has no eyes.

[...]
added.
Quote:
>
--
Jorge.

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #4  
Old October 12th, 2008, 03:25 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 12, 12:00*am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
FAQ Topic - Why does K = parseInt('09') set K to 0?
Quote:
Method ` parseInt ` generally needs a
second parameter ` radix ` for the base.
>
Function ` parseInt ` decides what base to convert a
number represented as a string to by looking at the string. Any
string beginning with ` '0x' ` or ` '0X' ` represents a
hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
parameter: ` parseInt("09", base) *`

That needs to be completely rewritten, spec to hand. For example :-


Method ` parseInt ` is case=independent. Leading whitespace is
ignored. A leading sign, + or -, is then accepted. The first
character which cannot be part of the number in the selected base
terminates reading.

The method generally needs a second parameter, range 2 to 36, for the
base : ` parseInt("09", base) ` .

Otherwise, leading ` '0X' ` indicates hexadecimal. Otherwise, leading
zero indicates either octal or decimal; octal is common, decimal is
preferred. Otherwise, the input is read as decimal.

So the string ` '09' ` will commonly give Number 0 (the octal digits
are ` 0-7 `);
but may give Number 9.

For details, see ISO/IEC 16262 Section 15.1.2.2.

For decimal strings without trailing non-whitespace, unary + (or -)
should be used : +"09" gives Number 9.



*** I strongly suggest that, after the daily post of a particular FAQ
section, and excepr for the correction of major or trivial errors, the
master copy of that section should NOT be changed for seven days, and
longer if the valuable part of the discussion is still active. Then
it should be changed as then seems best to the maintainer. Otherwise,
there will be confusion. ***


ASIDE : The standard implies, ISTM, that if parseInt("09")
gives 0 then parseInt("0") uses octal <G>.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
  #5  
Old October 12th, 2008, 03:25 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 12, 4:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
-----------------------------------------------------------------------
FAQ Topic - Why does K = parseInt('09') set K to 0?
-----------------------------------------------------------------------
>
[snip]
>
assuming decimal, ` '09' ` will be converted to 9 (the leading
zero is ignored).
Actually, as per the specification, the leading zero is not
technically ignored but forms a part of the calculation. For
parseInt("012", 10):

"012" -0 * 10^2 + 1 * 10^1 + 2 * 10^0 [ECMA-262-3rd-edition;
15.1.2.2; step 16]

Also, it's a surprise that most of the popular implementations go
against the `encouraged implementation' when dealing with missing
`radix'.

"When radix is 0 or undefined and the string's number begins with a 0
digit not followed by an x or X, then the implementation may, at its
discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this
case as being decimal."

/sasuke
  #6  
Old October 12th, 2008, 10:35 PM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

Dr J R Stockton wrote:
Quote:
On Oct 12, 12:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
>
Quote:
>FAQ Topic - Why does K = parseInt('09') set K to 0?
>
Quote:
>Method ` parseInt ` generally needs a
>second parameter ` radix ` for the base.
>>
>Function ` parseInt ` decides what base to convert a
>number represented as a string to by looking at the string. Any
>string beginning with ` '0x' ` or ` '0X' ` represents a
>hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
>parameter: ` parseInt("09", base) `
>
>
That needs to be completely rewritten, spec to hand. For example :-
>
>
Method ` parseInt ` is case=independent. Leading whitespace is
ignored. A leading sign, + or -, is then accepted. The first
character which cannot be part of the number in the selected base
terminates reading.
>
The method generally needs a second parameter, range 2 to 36, for the
base : ` parseInt("09", base) ` .
>
This para should be first. It's the quickest answer to the question.

Quote:
Otherwise, leading ` '0X' ` indicates hexadecimal. Otherwise, leading
zero indicates either octal or decimal; octal is common, decimal is
preferred. Otherwise, the input is read as decimal.
>
So the string ` '09' ` will commonly give Number 0 (the octal digits
are ` 0-7 `);
but may give Number 9.
>
For details, see ISO/IEC 16262 Section 15.1.2.2.
>
The ECMA-262 r3 specification -link- section 15.1.2.2.


Quote:
For decimal strings without trailing non-whitespace, unary + (or -)
should be used : +"09" gives Number 9.
>
>
That has a different result in a few cases. I don't want to go into
details in the FAQ, but here are a few:
* Function parseInt converts to an integer value, unary + does not.
Example: +"9.09" results 9.09
* To chop off the decimal using binary |, the number goes through
ToInt32. Example: +"9.09"|0. Result: 9.
* Function parseInt chops off the extra non-number stuff at the end,
unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
parseInt, the result is 10, with +"10px" the result is NaN. But if
parseInt encounters an 'e', it's not interpreted as scientific notation,
where as with unary +, it is.

In many cases, unary + will work, but in many cases, it might not
("10px" would be a common case where it would fail).

The mozilla site has few simple examples demonstrating that. The notes
article goes into detail on these.
Quote:
>
*** I strongly suggest that, after the daily post of a particular FAQ
section, and excepr for the correction of major or trivial errors, the
master copy of that section should NOT be changed for seven days, and
longer if the valuable part of the discussion is still active. Then
it should be changed as then seems best to the maintainer. Otherwise,
there will be confusion. ***
>
OK.

For this one, we can compare what I have now, what you've written, what
existed before (as it appears in the thread). What I have now:-


| Method parseInt generally needs a second parameter, radix, for the
| base (value between 2 and 36).
|
| If radix is omitted, the base is determined by the contents of the
| string. Any string beginning with '0x' or '0X' represents a
| hexadecimal number. A string beginning with a leading 0 is parsed as
| octal (octal digits are 0-7). The string '09' is converted to 0.
|
| To force use of a particular base, use the radix parameter:
| parseInt("09", base). If base 10 is desired, the unary + operator can
| be an option. Example: var s = '-09.1'; // Input string. var j = +s;
| // Convert to number. Result: -9.1 var n = j|0; // Chop off decimal
| (convert ToInt32). Result: -9


It might be better using:

"Method |parseInt(string , radix)| generally..."

Add: "leading whitespace is ignored".
Quote:
>
ASIDE : The standard implies, ISTM, that if parseInt("09")
gives 0 then parseInt("0") uses octal <G>.
>
0 is 0. Why does this matter?

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #7  
Old October 13th, 2008, 01:05 AM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

dhtml wrote:
Quote:
Dr J R Stockton wrote:
Quote:
>On Oct 12, 12:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
>>
Quote:
>>FAQ Topic - Why does K = parseInt('09') set K to 0?
>>
Quote:
>>Method ` parseInt ` generally needs a
>>second parameter ` radix ` for the base.
>>>
>>Function ` parseInt ` decides what base to convert a
>>number represented as a string to by looking at the string. Any
>>string beginning with ` '0x' ` or ` '0X' ` represents a
>>hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
>>parameter: ` parseInt("09", base) `
>>
>>
>That needs to be completely rewritten, spec to hand. For example :-
>>
>>
>Method ` parseInt ` is case=independent. Leading whitespace is
>ignored. A leading sign, + or -, is then accepted. The first
>character which cannot be part of the number in the selected base
>terminates reading.
>>
>The method generally needs a second parameter, range 2 to 36, for the
>base : ` parseInt("09", base) ` .
>>
>
This para should be first. It's the quickest answer to the question.
>
>
Quote:
>Otherwise, leading ` '0X' ` indicates hexadecimal. Otherwise, leading
>zero indicates either octal or decimal; octal is common, decimal is
>preferred. Otherwise, the input is read as decimal.
>>
>So the string ` '09' ` will commonly give Number 0 (the octal digits
>are ` 0-7 `);
>but may give Number 9.
>>
>For details, see ISO/IEC 16262 Section 15.1.2.2.
>>
>
The ECMA-262 r3 specification -link- section 15.1.2.2.
>
>
>
Quote:
>For decimal strings without trailing non-whitespace, unary + (or -)
>should be used : +"09" gives Number 9.
>>
>>
>
That has a different result in a few cases. I don't want to go into
details in the FAQ, but here are a few:
* Function parseInt converts to an integer value, unary + does not.
Example: +"9.09" results 9.09
* To chop off the decimal using binary |, the number goes through
ToInt32. Example: +"9.09"|0. Result: 9.
* Function parseInt chops off the extra non-number stuff at the end,
unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
parseInt, the result is 10, with +"10px" the result is NaN. But if
parseInt encounters an 'e', it's not interpreted as scientific notation,
where as with unary +, it is.
>
In many cases, unary + will work, but in many cases, it might not
("10px" would be a common case where it would fail).
>
The mozilla site has few simple examples demonstrating that. The notes
article goes into detail on these.
>
Quote:
>>
>*** I strongly suggest that, after the daily post of a particular FAQ
>section, and excepr for the correction of major or trivial errors, the
>master copy of that section should NOT be changed for seven days, and
>longer if the valuable part of the discussion is still active. Then
>it should be changed as then seems best to the maintainer. Otherwise,
>there will be confusion. ***
>>
>
OK.
>
For this one, we can compare what I have now, what you've written, what
existed before (as it appears in the thread). What I have now:-
>
>
| Method parseInt generally needs a second parameter, radix, for the
| base (value between 2 and 36).
|
| If radix is omitted, the base is determined by the contents of the
| string. Any string beginning with '0x' or '0X' represents a
| hexadecimal number. A string beginning with a leading 0 is parsed as
| octal (octal digits are 0-7). The string '09' is converted to 0.
|
| To force use of a particular base, use the radix parameter:
| parseInt("09", base).



[omit]
Quote:
If base 10 is desired, the unary + operator can
| be an option. Example: var s = '-09.1'; // Input string. var j = +s;
| // Convert to number. Result: -9.1 var n = j|0; // Chop off decimal
| (convert ToInt32). Result: -9
>
>
[/omit]

Omitting that last sentence bit would make the text shorter, while still
answering the question.





--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #8  
Old October 13th, 2008, 05:25 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 12, 7:19*pm, Dr J R Stockton <J.R.Stock...@physics.orgwrote:
Quote:
On Oct 12, 12:00*am, "FAQ server" <javascr...@dotinternet.bewrote:
>
Quote:
FAQ Topic - Why does K = parseInt('09') set K to 0?
Method ` parseInt ` generally needs a
second parameter ` radix ` for the base.
>
Quote:
Function ` parseInt ` decides what base to convert a
number represented as a string to by looking at the string. Any
string beginning with ` '0x' ` or ` '0X' ` represents a
hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
parameter: ` parseInt("09", base) *`
>
That needs to be completely rewritten, spec to hand. *For example :-
>
Method ` parseInt ` is case=independent. *Leading whitespace is
ignored. *A leading sign, + or -, is then accepted. *The first
character which cannot be part of the number in the selected base
terminates reading.
>
The method generally needs a second parameter, range 2 to 36, for the
base *: ` parseInt("09", base) *` .
>
Otherwise, leading ` '0X' ` indicates hexadecimal. *Otherwise, leading
zero indicates either octal or decimal; octal is common, decimal is
preferred. *Otherwise, the input is read as decimal.
>
So the string ` '09' ` will commonly give Number 0 (the octal digits
are ` 0-7 `);
but may give Number 9.
>
For details, see ISO/IEC 16262 Section 15.1.2.2.
>
For decimal strings without trailing non-whitespace, unary + (or -)
should be used : +"09" gives Number 9.
>
*** I strongly suggest that, after the daily post of a particular FAQ
section, and excepr for the correction of major or trivial errors, the
master copy of that section should NOT be changed for seven days, and
longer if the valuable part of the discussion is still active. *Then
it should be changed as then seems best to the maintainer. *Otherwise,
there will be confusion. ***
>
ASIDE : The standard implies, ISTM, that if parseInt("09")
* * * * gives 0 then parseInt("0") uses octal <G>.
>
--
* (c) John Stockton, near London, UK. *Posting with Google.
*Mail: J.R.""""""""@physics.org or (better) via Home Page at
*Web: *<URL:http://www.merlyn.demon.co.uk/>
*FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|
  #9  
Old October 13th, 2008, 05:35 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 12, 7:19 pm, Dr J R Stockton <J.R.Stock...@physics.orgwrote:
Quote:
ASIDE : The standard implies, ISTM, that if parseInt("09")
gives 0 then parseInt("0") uses octal <G>.
The standard recommends otherwise:
"When radix is 0 or undefined and the string's number begins with a 0
digit not followed by an x or X, then the implementation may, at its
discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this
case as being decimal."

/sasuke
  #10  
Old October 13th, 2008, 06:35 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

In comp.lang.javascript message <gctq2h$om0$1@registered.motzarella.org>
, Sun, 12 Oct 2008 14:26:38, dhtml <dhtmlkitchen@gmail.composted:
Quote:
>Dr J R Stockton wrote:
Quote:
>On Oct 12, 12:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
>>
Quote:
>>FAQ Topic - Why does K = parseInt('09') set K to 0?
>>
Quote:
>>Method ` parseInt ` generally needs a
>>second parameter ` radix ` for the base.
>>>
>>Function ` parseInt ` decides what base to convert a
>>number represented as a string to by looking at the string. Any
>>string beginning with ` '0x' ` or ` '0X' ` represents a
>>hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
>>parameter: ` parseInt("09", base) `
> That needs to be completely rewritten, spec to hand. For example
>>:-
> Method ` parseInt ` is case=independent. Leading whitespace is
>ignored. A leading sign, + or -, is then accepted. The first
>character which cannot be part of the number in the selected base
>terminates reading.
> The method generally needs a second parameter, range 2 to 36, for
>>the
>base : ` parseInt("09", base) ` .
>>
>
>This para should be first. It's the quickest answer to the question.
If you wish. But it's better for them to read the whole article;
perhaps it should be last.

Quote:
Quote:
>Otherwise, leading ` '0X' ` indicates hexadecimal. Otherwise, leading
>zero indicates either octal or decimal; octal is common, decimal is
>preferred. Otherwise, the input is read as decimal.
> So the string ` '09' ` will commonly give Number 0 (the octal digits
>are ` 0-7 `);
>but may give Number 9.
> For details, see ISO/IEC 16262 Section 15.1.2.2.
>>
>
>The ECMA-262 r3 specification -link- section 15.1.2.2.
>
>
>
Quote:
>For decimal strings without trailing non-whitespace, unary + (or -)
>should be used : +"09" gives Number 9.
>>
>
>That has a different result in a few cases. I don't want to go into
>details in the FAQ, but here are a few:
* Function parseInt converts to an integer value, unary + does not.
>Example: +"9.09" results 9.09
Yes, I omitted the word "integer" or "digit".
Quote:
* To chop off the decimal using binary |, the number goes through
>ToInt32. Example: +"9.09"|0. Result: 9.
A parseInt article must imply that parseInt truncates number-strings
such as "12.34" and "34e5". But we should do nothing to encourage such
use. I'd not mention |0 in it, though it's worth having somewhere. Add
an answer on logic operations with Numbers?
Quote:
* Function parseInt chops off the extra non-number stuff at the end,
>unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
>parseInt, the result is 10, with +"10px" the result is NaN.
A sentence in my first paragraph includes "terminates".



Quote:
Quote:
> ASIDE : The standard implies, ISTM, that if parseInt("09")
> gives 0 then parseInt("0") uses octal <G>.
>
>0 is 0. Why does this matter?
It does not matter; it's an aside, with <G>. Some might be mildly
amused to see how they were unwittingly using octal.


--
(c) John Stockton, nr London, 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.
  #11  
Old October 13th, 2008, 11:05 PM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

Dr J R Stockton wrote:
Quote:
In comp.lang.javascript message <gctq2h$om0$1@registered.motzarella.org>
, Sun, 12 Oct 2008 14:26:38, dhtml <dhtmlkitchen@gmail.composted:
Quote:
>Dr J R Stockton wrote:
Quote:
>>On Oct 12, 12:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
>>>
>>>FAQ Topic - Why does K = parseInt('09') set K to 0?
>>>Method ` parseInt ` generally needs a
>>>second parameter ` radix ` for the base.
>>>>
>>>Function ` parseInt ` decides what base to convert a
>>>number represented as a string to by looking at the string. Any
>>>string beginning with ` '0x' ` or ` '0X' ` represents a
>>>hexadecimal number. A string beginning with a leading ` 0 ` 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, use the radix
>>>parameter: ` parseInt("09", base) `
>> That needs to be completely rewritten, spec to hand. For example
>>:-
>> Method ` parseInt ` is case=independent. Leading whitespace is
>>ignored. A leading sign, + or -, is then accepted. The first
>>character which cannot be part of the number in the selected base
>>terminates reading.
>> The method generally needs a second parameter, range 2 to 36, for
>>the
>>base : ` parseInt("09", base) ` .
>>>
>This para should be first. It's the quickest answer to the question.
>
If you wish. But it's better for them to read the whole article;
perhaps it should be last.
>
>
Quote:
Quote:
>>Otherwise, leading ` '0X' ` indicates hexadecimal. Otherwise, leading
>>zero indicates either octal or decimal; octal is common, decimal is
>>preferred. Otherwise, the input is read as decimal.
>> So the string ` '09' ` will commonly give Number 0 (the octal digits
>>are ` 0-7 `);
>>but may give Number 9.
>> For details, see ISO/IEC 16262 Section 15.1.2.2.
>>>
>The ECMA-262 r3 specification -link- section 15.1.2.2.
>>
>>
>>
Quote:
>>For decimal strings without trailing non-whitespace, unary + (or -)
>>should be used : +"09" gives Number 9.
>>>
>That has a different result in a few cases. I don't want to go into
>details in the FAQ, but here are a few:
>* Function parseInt converts to an integer value, unary + does not.
>Example: +"9.09" results 9.09
>
Yes, I omitted the word "integer" or "digit".
>
Unary + can be an alternative, but not always.
Quote:
Quote:
>* To chop off the decimal using binary |, the number goes through
>ToInt32. Example: +"9.09"|0. Result: 9.
>
A parseInt article must imply that parseInt truncates number-strings
such as "12.34" and "34e5". But we should do nothing to encourage such
use. I'd not mention |0 in it, though it's worth having somewhere. Add
an answer on logic operations with Numbers?
>
Quote:
>* Function parseInt chops off the extra non-number stuff at the end,
>unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
>parseInt, the result is 10, with +"10px" the result is NaN.
>
A sentence in my first paragraph includes "terminates".
>
Proposed text:

| Method parseInt generally needs a second parameter, radix, for the
| base (value between 2 and 36).
|
| Leading whitespace is ignored, leading sign, + or -, is then accepted.
|
| If radix is omitted, the base is determined by the contents of the
| remainder of the string. Any number beginning with '0x' or '0X'
| represents a hexadecimal number. A number beginning with a leading 0
| may be parsed as octal (octal digits are 0-7).
|
| The result stops at the first non-numeric character. If the result
| is empty, NaN is returned.
|
| To force use of a particular base, use the radix parameter:
| parseInt("09", base).
|
| For decimal strings without trailing non-whitespace, unary + (or -)
| should be used : +"09.1" gives Number 9.1


Taking into account that using octal for string w/leading '0' is
optional, truncating the result at the first non-numeric character, and
using a decimal string for the unary example.

What do you think?

Garrett

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #12  
Old October 13th, 2008, 11:45 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

In comp.lang.javascript message <384e33b7-94a2-49bf-ba67-10203ec57da8@25
g2000prz.googlegroups.com>, Mon, 13 Oct 2008 09:25:32, sasuke
<database666@gmail.composted:
Quote:
>On Oct 12, 7:19 pm, Dr J R Stockton <J.R.Stock...@physics.orgwrote:
Quote:
>ASIDE : The standard implies, ISTM, that if parseInt("09")
> gives 0 then parseInt("0") uses octal <G>.
>
>The standard recommends otherwise:
>"When radix is 0 or undefined and the string's number begins with a 0
>digit not followed by an x or X, then the implementation may, at its
>discretion, interpret the number either as being octal or as being
>decimal. Implementations are encouraged to interpret numbers in this
>case as being decimal."
Perhaps you are not a native English speaker. Read what I wrote more
carefully, particularly the word "if"; and consider the difference
between implication and recommendation.

--
(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.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
  #13  
Old October 14th, 2008, 01:45 AM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

Dr J R Stockton wrote:
Quote:
In comp.lang.javascript message <384e33b7-94a2-49bf-ba67-10203ec57da8@25
g2000prz.googlegroups.com>, Mon, 13 Oct 2008 09:25:32, sasuke
<database666@gmail.composted:
Quote:
>On Oct 12, 7:19 pm, Dr J R Stockton <J.R.Stock...@physics.orgwrote:
Quote:
>>ASIDE : The standard implies, ISTM, that if parseInt("09")
>> gives 0 then parseInt("0") uses octal <G>.
>The standard recommends otherwise:
>"When radix is 0 or undefined and the string's number begins with a 0
>digit not followed by an x or X, then the implementation may, at its
>discretion, interpret the number either as being octal or as being
>decimal. Implementations are encouraged to interpret numbers in this
>case as being decimal."
>
Perhaps you are not a native English speaker. Read what I wrote more
carefully, particularly the word "if"; and consider the difference
between implication and recommendation.
>
No he was correcting me. I had:
| A string beginning with a leading 0 *is* parsed as octal (octal digits
|are 0-7).


Changed proposed text to:
| A number beginning with a leading 0 *may* be parsed as octal (octal
| digits are 0-7).


--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #14  
Old October 14th, 2008, 06:25 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

In comp.lang.javascript message <gd0gk5$tss$1@registered.motzarella.org>
, Mon, 13 Oct 2008 15:03:48, dhtml <dhtmlkitchen@gmail.composted:
Quote:
>Dr J R Stockton wrote:
Quote:
>In comp.lang.javascript message <gctq2h$om0$1@registered.motzarella.org>
>, Sun, 12 Oct 2008 14:26:38, dhtml <dhtmlkitchen@gmail.composted:
Quote:
Quote:
Quote:
>>>For decimal strings without trailing non-whitespace, unary + (or -)
>>>should be used : +"09" gives Number 9.
> Yes, I omitted the word "integer" or "digit".
>Unary + can be an alternative, but not always.
For decimal digit strings without trailing non-whitespace, when is unary
+ inappropriate?

Quote:
Quote:
Quote:
>>* Function parseInt chops off the extra non-number stuff at the end,
>>unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
>>parseInt, the result is 10, with +"10px" the result is NaN.
> A sentence in my first paragraph includes "terminates".
Unary + does more than not chop them; any non-whitespace after the first
non-digit after a digit produces NaN.

Quote:
>Proposed text:
Quote:
>| If radix is omitted, the base is determined by the contents of the
^ but not only if xxxxxxxxxxxxxxx
Quote:
>| remainder of the string. Any number beginning with '0x' or '0X'
>| represents a hexadecimal number. A number beginning with a leading 0
>| may be parsed as octal (octal digits are 0-7).
>|
>| The result stops at the first non-numeric character. If the result
If radix is 3, conversion stops at 2, which is not very non-numeric.

The string is not a Number. There are no numbers in the string; only
digits and other characters. The result is a Number. It's maybe best
to use the letters "number" to refer only to IEEE Doubles, and then to
make it "Number".

How about a hidden FAQ note (URL in comment) noting any policy decisions
that may not be obvious?

Quote:
>Taking into account that using octal for string w/leading '0' is
>optional, truncating the result at the first non-numeric character, and
>using a decimal string for the unary example.
Being practical takes precedence over being slavishly adherent to the
formal standards : do we know of any browsers in actual use for which
parseInt("09") gives Number 9?

Quote:
>What do you think?
My own present version is in volatile non-linked temporary page */js-
faq.* : to avoid Web-scanning, you'll have to guess the rest of the URL.


--
(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.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
  #15  
Old October 14th, 2008, 06:35 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 14, 6:45*pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
Quote:
Perhaps you are not a native English speaker. Read what I wrote more
carefully, particularly the word "if"; and consider the difference
between implication and recommendation.
Rightly said, I am not, though I have been trying hard to get it
right.
Quote:
Quote:
Taking into account that using octal for string w/leading '0' is
optional, truncating the result at the first non-numeric character, and
using a decimal string for the unary example.
>
Being practical takes precedence over being slavishly adherent to the
formal standards : do we know of any browsers in actual use for which
parseInt("09") gives Number 9?
Yes, Opera 9.21 which is pretty much in actual use[as expected; Opera
seems to be better at following standards]
Quote:
My own present version is in volatile non-linked temporary page */js-
faq.* : to avoid Web-scanning, you'll have to guess the rest of the URL.
Maybe presenting the URL as "www#google#com" would have been much
easier on Garret.

/sasuke
  #16  
Old October 14th, 2008, 08:15 PM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

Dr J R Stockton wrote:
Quote:
In comp.lang.javascript message <gd0gk5$tss$1@registered.motzarella.org>
, Mon, 13 Oct 2008 15:03:48, dhtml <dhtmlkitchen@gmail.composted:
Quote:
>Dr J R Stockton wrote:
Quote:
>>In comp.lang.javascript message <gctq2h$om0$1@registered.motzarella.org>
>>, Sun, 12 Oct 2008 14:26:38, dhtml <dhtmlkitchen@gmail.composted:
>
Quote:
Quote:
>>>>For decimal strings without trailing non-whitespace, unary + (or -)
>>>>should be used : +"09" gives Number 9.
>> Yes, I omitted the word "integer" or "digit".
>Unary + can be an alternative, but not always.
>
For decimal digit strings without trailing non-whitespace, when is unary
+ inappropriate?
>
If an integer value is desired. Say input is "9.01". +"9.01" results in
9.01; parseInt results in 9.
Quote:
>
Quote:
Quote:
>>>* Function parseInt chops off the extra non-number stuff at the end,
>>>unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
>>>parseInt, the result is 10, with +"10px" the result is NaN.
>> A sentence in my first paragraph includes "terminates".
>
Unary + does more than not chop them; any non-whitespace after the first
non-digit after a digit produces NaN.
Right. And I wrote: /"with +"10px" the result is NaN"/.
Quote:
>
>
Quote:
>Proposed text:
>
Quote:
>| If radix is omitted, the base is determined by the contents of the
^ but not only if xxxxxxxxxxxxxxx
Using 'if and only if' would be more technically correct but what follows:

| To force use of a particular base, use the radix parameter:
| parseInt("09", base).

Quote:
Quote:
>| remainder of the string. Any number beginning with '0x' or '0X'
>| represents a hexadecimal number. A number beginning with a leading 0
>| may be parsed as octal (octal digits are 0-7).
>|
>| The result stops at the first non-numeric character. If the result
>
If radix is 3, conversion stops at 2, which is not very non-numeric.
>
The string is not a Number.
Right.

There are no numbers in the string; only
Quote:
digits and other characters. The result is a Number. It's maybe best
to use the letters "number" to refer only to IEEE Doubles, and then to
make it "Number".
>
Using 'Number' would seem like a Number object. That would be wrong.
Quote:
How about a hidden FAQ note (URL in comment) noting any policy decisions
that may not be obvious?
>
>
Quote:
>Taking into account that using octal for string w/leading '0' is
>optional, truncating the result at the first non-numeric character, and
>using a decimal string for the unary example.
>
Being practical takes precedence over being slavishly adherent to the
formal standards : do we know of any browsers in actual use for which
parseInt("09") gives Number 9?
>
>
Quote:
>What do you think?
>
My own present version is in volatile non-linked temporary page */js-
faq.* : to avoid Web-scanning, you'll have to guess the rest of the URL.
>
I cannot bring up your website.

Revised:

| Method parseInt generally needs a second parameter, radix, for the
| base (value between 2 and 36).
|
| Leading whitespace is ignored, leading sign, + or -, is then accepted.
|
| If radix is omitted, the base is determined by the contents of the
| string. Any string beginning with '0x' or '0X' represents a
| hexadecimal number. Most implementations parse a string beginning with
| 0 as octal (octal digits are 0-7).
|
| The algorithm stops at the first character not included in the
| base. If the result is empty, NaN is returned.
|
| To force use of a particular base, use the radix parameter:
| parseInt("09", base).
|

Garrett
Quote:
>

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #17  
Old October 14th, 2008, 08:25 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 15, 12:11*am, dhtml <dhtmlkitc...@gmail.comwrote:
Quote:
Quote:
digits and other characters. *The result is a Number. *It's maybe best
to use the letters "number" to refer only to IEEE Doubles, and then to
make it "Number".
>
Using 'Number' would seem like a Number object. That would be wrong.
How about explicitly using the terminology used by ECMAScript wherein
a clear distinction is made between the 'Number value', 'Number Type'
and 'Number Object [which is of type Object Type]'?

/sasuke
  #18  
Old October 15th, 2008, 06:15 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

In comp.lang.javascript message <979f7a39-54d8-431c-8a8a-a62c9eafac6b@q2
6g2000prq.googlegroups.com>, Tue, 14 Oct 2008 10:30:05, sasuke
<database666@gmail.composted:
Quote:
>On Oct 14, 6:45*pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
Quote:
Quote:
>Being practical takes precedence over being slavishly adherent to the
>formal standards : do we know of any browsers in actual use for which
>parseInt("09") gives Number 9?
>
>Yes, Opera 9.21 which is pretty much in actual use[as expected; Opera
>seems to be better at following standards]
Confirmed in 9.27, 9.60. Actually, I wasn't expecting that. Not in
IE7, FF3, Sf3, Chrome. Perhaps the FAQ should mention Opera.
Quote:
Quote:
>My own present version is in volatile non-linked temporary page */js-
>faq.* : to avoid Web-scanning, you'll have to guess the rest of the URL.
>
>Maybe presenting the URL as "www#google#com" would have been much
>easier on Garret.
I think you underestimate Garrett. The site is in the sig.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
  #19  
Old October 15th, 2008, 06:15 PM
Dr J R Stockton
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

In comp.lang.javascript message <gd2qt6$ekv$1@registered.motzarella.org>
, Tue, 14 Oct 2008 12:11:32, dhtml <dhtmlkitchen@gmail.composted:
Quote:
>Dr J R Stockton wrote:
Quote:
>In comp.lang.javascript message <gd0gk5$tss$1@registered.motzarella.org>
>, Mon, 13 Oct 2008 15:03:48, dhtml <dhtmlkitchen@gmail.composted:
Quote:
Quote:
> For decimal digit strings without trailing non-whitespace, when is
>>unary
>+ inappropriate?
>>
>
>If an integer value is desired. Say input is "9.01". +"9.01" results in
>9.01; parseInt results in 9.
But "9.01" is not a decimal digit string; it contains a ".", which is a
non-digit. I know that there can be leading whitespace and sign; but
the sentence is true without mentioning that.

Quote:
Quote:
Quote:
>>>>* Function parseInt chops off the extra non-number stuff at the
>>>>>end,
>>>>unary + doesn't. For example: parseInt("10px", 10), vs +"10px", with
>>>>parseInt, the result is 10, with +"10px" the result is NaN.
>>> A sentence in my first paragraph includes "terminates".
> Unary + does more than not chop them; any non-whitespace after the
>>first
>non-digit after a digit produces NaN.
>
>Right. And I wrote: /"with +"10px" the result is NaN"/.
>
Quote:
>>
Quote:
>>Proposed text:
>>
Quote:
>>| If radix is omitted, the base is determined by the contents of the
> ^ but not only if xxxxxxxxxxxxxxx
>
>Using 'if and only if' would be more technically correct but what follows:
It would not. Look up the effect of radix zero. In IE7 at least, an
undefined or null radix also allows choice. So does Infinity, which may
be a bug.


Quote:
>I cannot bring up your website.
The link in any of my news sigs should work. The most economical test
page (albeit technically invalid) is $.htm, an empty file.

Quote:
>| Method parseInt generally needs a second parameter, radix, for the
>| base (value between 2 and 36).
That "between" implies the range 3, 4, ..., 35. "(2 to 36)" is better.


--
(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.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
  #20  
Old October 16th, 2008, 05:55 AM
dhtml
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

sasuke wrote:
Quote:
On Oct 12, 4:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
>-----------------------------------------------------------------------
>FAQ Topic - Why does K = parseInt('09') set K to 0?
>-----------------------------------------------------------------------
Quote:
Also, it's a surprise that most of the popular implementations go
against the `encouraged implementation' when dealing with missing
`radix'.
>
Are they afraid of breaking pages?
Quote:
"When radix is 0 or undefined and the string's number begins with a 0
digit not followed by an x or X, then the implementation may, at its
discretion, interpret the number either as being octal or as being
decimal. Implementations are encouraged to interpret numbers in this
case as being decimal."
>

This function is not awful; it's horrible.

The radix goes through ToInt32. When the result is 0, radix is
determined by the string. Following that, if the string begins with '0x'
or '0X' it is hexadecimal, otherwise, if it begins with just '0', it is
implementation dependent - either base 8 or base 10.

This will happen when radix is 0, "0", NaN, false, undefined, null,
-Infinity, or any object that converts to those values, such as an
invalid date.

// Result: 9 or 0.
parseInt('09', NaN);
parseInt('09', -Infinity);
parseInt('09', undefined);
parseInt('09', new Date("foo"));
parseInt('09', [0]);
parseInt('09', {});


When radix is (or is converted to) a number N where N <= -1 or N == 1,
or N 36,

// Result: 9.
parseInt(10, [10]);
parseInt(10, { valueOf : function() { return 10; } });
parseInt('09', 10.1);

Otherwise, radix is out of range,

// Result: NaN
parseInt(10, -1);
parseInt(10, 1);
parseInt(10, 37);


So, the "quick answer": UAFR

(you figure it out).

A little longer:

| Function parseInt needs a radix. If a radix is omitted the
| implementation may iterpret the input as octal if it starts with 0.
|
| Example: parseInt('09', 10); // Result 9.
|


Quote:
/sasuke

--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
  #21  
Old October 16th, 2008, 04:15 PM
sasuke
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does K = parseInt('09') set K to 0? (2008-10-12)

On Oct 16, 9:51*am, dhtml <dhtmlkitc...@gmail.comwrote:
Quote:
This function is not awful; it's horrible.
>
The radix goes through ToInt32. When the result is 0, radix is
determined by the string. Following that, if the string begins with '0x'
or '0X' it is hexadecimal, otherwise, if it begins with just '0', it is
implementation dependent - either base 8 or base 10.
>
This will happen when radix is 0, "0", NaN, false, undefined, null,
-Infinity, or any object that converts to those values, such as an
invalid date.
>
// Result: 9 or 0.
parseInt('09', NaN);
parseInt('09', -Infinity);
parseInt('09', undefined);
parseInt('09', new Date("foo"));
parseInt('09', [0]);
parseInt('09', {});
>
When radix is (or is converted to) a number N where N <= -1 or N ==1,
or N 36,
>
// Result: 9.
parseInt(10, [10]);
parseInt(10, { valueOf : function() { return 10; } });
parseInt('09', 10.1);
>
Otherwise, radix is out of range,
>
// Result: NaN
parseInt(10, -1);
parseInt(10, 1);
parseInt(10, 37);
Interesting; having this information is the FAQ would definitely serve
as an exhaustive resource when dealing with `parseInt'.

/sasuke

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On