FAQ Topic - Why does parseInt('09') give an error?
Question posted by: FAQ server
(Guest)
on
August 24th, 2006 10:55 PM
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
|
|
August 25th, 2006 08:05 PM
# 2
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <44ee2f77$0$75029$14726298@news.sunsite.dk>, dated Thu,
24 Aug 2006 23:00:01 remote, seen in news:comp.lang.javascript, FAQ
server <javascript@dotinternet.beposted :
Quote:
Originally Posted by
>-----------------------------------------------------------------------
>FAQ Topic - Why does parseInt('09') give an error?
>-----------------------------------------------------------------------
>
>The parseInt function decides what base the number is by looking
>at the number. By convention it assumes that any number beginning
>with 0x is Hexadecimal, and otherwise any number beginning with
>0 is Octal. To force use of base 10 add a second parameter
>`` parseInt("09",10) ''
|
.... or use +"09" .
IMHO, parseInt should be used only when at least one of these applies :-
The base is neither 10, nor 16 indicated by 0x
The base is variable
The string may have trailing non-whitespace
The string may be empty, to give NaN not 0.
In particular, it should not be used for a match to /^\s*\d+\s*$/
That may be incomplete or sub-optimal - think about exceptions such as
an empty string.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
August 25th, 2006 08:35 PM
# 3
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 8/25/2006 8:13 AM:
Quote:
Originally Posted by
JRS: In article <44ee2f77$0$75029$14726298@news.sunsite.dk>, dated Thu,
24 Aug 2006 23:00:01 remote, seen in news:comp.lang.javascript, FAQ
server <javascript@dotinternet.beposted :
Quote:
Originally Posted by
>-----------------------------------------------------------------------
>FAQ Topic - Why does parseInt('09') give an error?
>-----------------------------------------------------------------------
>>
>The parseInt function decides what base the number is by looking
>at the number. By convention it assumes that any number beginning
>with 0x is Hexadecimal, and otherwise any number beginning with
>0 is Octal. To force use of base 10 add a second parameter
>`` parseInt("09",10) ''
|
>
.... or use +"09" .
>
>
>
>
IMHO, parseInt should be used only when at least one of these applies :-
The base is neither 10, nor 16 indicated by 0x
|
Why exclude base 16 but not base 8?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
August 26th, 2006 05:45 PM
# 4
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <xI2dnXN_zbdhwnLZnZ2dnUVZ_tSdnZ2d@comcast.com>, dated
Fri, 25 Aug 2006 16:39:35 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>Dr John Stockton said the following on 8/25/2006 8:13 AM:
|
Quote:
Originally Posted by
Quote:
Originally Posted by
>IMHO, parseInt should be used only when at least one of these applies :-
> The base is neither 10, nor 16 indicated by 0x
|
>
>Why exclude base 16 but not base 8?
|
That only excludes "base 16 indicated by 0x". A numeric string starting
"0x" should be interpreted as Hex, and will be by other methods. To
parseInt, it is zero in any base other than 16 34 35 36.
For bases 2..7, 9, 11..16, 17..36 parseInt must be used. Otherwise, a
string of non-negative integer value, after trimming non-numeric parts,
can be of the forms
2345 decimal unary + preferred
0123 decimal unary + preferred
0x6e4 hexadecimal unary + preferred
0123 octal must use parseInt
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
|
|
August 26th, 2006 06:35 PM
# 5
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton wrote:
[snip]
Quote:
Originally Posted by
A numeric string starting "0x" should be interpreted as Hex, and will
be by other methods. To parseInt, it is zero in any base other than
16 34 35 36.
|
And the leading zero is inconsequential for the latter three.
Quote:
Originally Posted by
For bases 2..7, 9, 11..16, 17..36 parseInt must be used.
|
You mean: 2..9, 11..15, and 17..36.
[snip]
Mike
|
|
August 27th, 2006 07:25 PM
# 6
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <EA0Ig.9248$r61.7934@text.news.blueyonder.co.uk>, dated
Sat, 26 Aug 2006 18:40:36 remote, seen in news:comp.lang.javascript,
Michael Winter <m.winter@blueyonder.co.ukposted :
Quote:
Originally Posted by
>Dr John Stockton wrote:
>
>[snip]
>
Quote:
Originally Posted by
>A numeric string starting "0x" should be interpreted as Hex, and will
>be by other methods. To parseInt, it is zero in any base other than
>16 34 35 36.
|
>
>And the leading zero is inconsequential for the latter three.
|
The leading zero itself is unimportant; the above refers to a leading
"0x". But the first sentence does need an "unless another base is
explicitly indicated".
Quote:
Originally Posted by
Quote:
Originally Posted by
>For bases 2..7, 9, 11..16, 17..36 parseInt must be used.
|
>
>You mean: 2..9, 11..15, and 17..36.
|
NO. The quoted line is exactly what I meant (apart from the obvious
16/15 typo); it deals with all bases for which both parseInt and a
second parameter are immediately and obviously necessary, which is those
other than 8, 10, and 16. It does NOT say anything about bases 8, 10,
16, leaving them for further consideration.
"A implies B" does not imply "not A implies not B" or "B
implies A" .
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
August 27th, 2006 08:05 PM
# 7
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton wrote:
Quote:
Originally Posted by
JRS: In article <EA0Ig.9248$r61.7934@text.news.blueyonder.co.uk>,
dated Sat, 26 Aug 2006 18:40:36 remote, seen in
news:comp.lang.javascript, Michael Winter <m.winter@blueyonder.co.uk>
posted :
>
Quote:
Originally Posted by
>Dr John Stockton wrote:
|
|
[snip]
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
>>For bases 2..7, 9, 11..16, 17..36 parseInt must be used.
|
>>
>You mean: 2..9, 11..15, and 17..36.
|
>
NO. The quoted line is exactly what I meant (apart from the obvious
16/15 typo); it deals with all bases for which both parseInt and a
second parameter are immediately and obviously necessary, which is
those other than 8, 10, and 16.
|
Only base-16 is guaranteed to be recognised automatically by the
parseInt function and only when "0x" prefixes the number; decimal is
assumed, otherwise. Octal may be recognised if the string begins with
zero (0), but that occurs at the discretion of the implementation. The
recommendation of the ECMAScript specification is /not/ to make octal a
special case, and there are browsers that follow that recommendation
(Opera, for example). Therefore, if one wishes to use the parseInt
function to convert an octal string to a number, the second argument
/is/ necessary.
[snip]
Mike
|
|
August 28th, 2006 10:35 PM
# 8
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <Y4nIg.9853$r61.9064@text.news.blueyonder.co.uk>, dated
Sun, 27 Aug 2006 20:16:56 remote, seen in news:comp.lang.javascript,
Michael Winter <m.winter@blueyonder.co.ukposted :
Quote:
Originally Posted by
>
>Only base-16 is guaranteed
|
Second draft :
For converting a (possibly signed) base-B digit string, S, to a Number,
function parseInt should be used only when beneficial, as alternatives
are longer or slower.
For values of numeric properties, given in decimal without leading zero
and possibly followed by a unit (e.g. 33px), parseInt(S) is appropriate.
It is obvious that bases 2..7, 9, 11..15, 17..36 require parseInt(S, B).
Otherwise, a string of non-negative integer value, after trimming
non-numeric parts, can be of the forms :
S B Conversion Note
0123 8 use parseInt(S, 8) 1
0123 10 unary + preferred
2345 10 unary + preferred
0x6b4 16 unary + preferred
6b4 16 use parseInt(S, 16)
Notes :
1: B is required for compatibility with ECMA 262 3rd Edn, and for
compatibility with all browsers.
TBD - consideration of use of minus.
--
© John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.
Don't quote more than is needed, and respond after each quoted part.
|
|
August 29th, 2006 06:05 AM
# 9
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton wrote:
Quote:
Originally Posted by
JRS: In article <Y4nIg.9853$r61.9064@text.news.blueyonder.co.uk>, dated
Sun, 27 Aug 2006 20:16:56 remote, seen in news:comp.lang.javascript,
Michael Winter <m.winter@blueyonder.co.ukposted :
Quote:
Originally Posted by
Only base-16 is guaranteed
|
>
Second draft :
>
For converting a (possibly signed) base-B digit string, S, to a Number,
|
I'd get rid of the parenthesis:
"For converting a possibly signed base-B digit string, S, to a
Number..."
Quote:
Originally Posted by
function parseInt should be used only when beneficial, as alternatives
are longer or slower.
|
That doesn't make sense - to me it reads "don't use parseInt because
it's faster and shorter than alternatives".
Did you really mean:
"function parseInt should be used only when beneficial, as
it is longer or slower than alternatives."
Quote:
Originally Posted by
For values of numeric properties, given in decimal without leading zero
and possibly followed by a unit (e.g. 33px), parseInt(S) is appropriate.
>
It is obvious that bases 2..7, 9, 11..15, 17..36 require parseInt(S, B).
|
Probably not to most. Less condescending is:
"Bases 2..7, 9, 11..15, 17..36 require parseInt(S, B)."
[...]
How about :
For converting a possibly signed base-B digit string, S, to a Number,
function parseInt should be used only when beneficial, as it is
longer or slower than alternatives.
For values of numeric properties, given in decimal without a leading
zero and possibly followed by a unit (such as when getting the value
of a style property, e.g. 33px), parseInt(S) is appropriate.
Bases 2 to 7, 9, 11 to 15, 17 to 36 require parseInt(S, B) always.
Bases 8, 10 and 16, where S is a string of non-negative integer value
and non-numeric parts have been trimmed (e.g. 09kg has been trimmed
to 09), can be of the forms:
S B Conversion Note
0123 8 use parseInt(S, 8) 1
0123 10 unary + preferred 2
2345 10 unary + preferred 2
0x6b4 16 unary + preferred
6b4 16 use parseInt(S, 16)
Notes :
1: B is required for compatibility with ECMA 262 3rd Edn, and
for compatibility with all browsers.
2: Common when converting the value of form controls to Number.
--
Rob
|
|
August 30th, 2006 05:25 PM
# 10
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <1156832149.528521.218420@74g2000cwt.googlegroups.c om>,
dated Mon, 28 Aug 2006 23:15:49 remote, seen in
news:comp.lang.javascript, RobG <rgqld@iinet.net.auposted :
Quote:
Originally Posted by
>Dr John Stockton wrote:
Quote:
Originally Posted by
>JRS: In article <Y4nIg.9853$r61.9064@text.news.blueyonder.co.uk>, dated
>Sun, 27 Aug 2006 20:16:56 remote, seen in news:comp.lang.javascript,
>Michael Winter <m.winter@blueyonder.co.ukposted :
|
|
Quote:
Originally Posted by
Quote:
Originally Posted by
>function parseInt should be used only when beneficial, as alternatives
>are longer or slower.
|
>
>That doesn't make sense - to me it reads "don't use parseInt because
>it's faster and shorter than alternatives".
|
It should make perfect nonsense, as I was concentrating on clarity and
totally forgot to check what might be called the polarity of the
statement :-( .
Quote:
Originally Posted by
>Did you really mean:
>
"function parseInt should be used only when beneficial, as
it is longer or slower than alternatives."
|
Well, maybe ... "as alternatives are shorter and faster."
Quote:
Originally Posted by
Quote:
Originally Posted by
>For values of numeric properties, given in decimal without leading zero
>and possibly followed by a unit (e.g. 33px), parseInt(S) is appropriate.
>>
>It is obvious that bases 2..7, 9, 11..15, 17..36 require parseInt(S, B).
|
>
>Probably not to most. Less condescending is:
>
"Bases 2..7, 9, 11..15, 17..36 require parseInt(S, B)."
|
But then someone like Randy will complain about 8 being missing, as
earlier in the thread. "... 17..36 clearly ..." ?
Quote:
Originally Posted by
>How about:
>
>For converting a possibly signed base-B digit string, S, to a Number,
>function parseInt should be used only when beneficial, as it is
>longer or slower than alternatives.
|
and
Quote:
Originally Posted by
>
>For values of numeric properties, given in decimal without a leading
>zero and possibly followed by a unit (such as when getting the value
>of a style property, e.g. 33px), parseInt(S) is appropriate.
>
>Bases 2 to 7, 9, 11 to 15, 17 to 36 require parseInt(S, B) always.
>
>Bases 8, 10 and 16, where S is a string of non-negative integer value
>and non-numeric parts have been trimmed (e.g. 09kg has been trimmed
>to 09), can be of the forms:
>
S B Conversion Note
0123 8 use parseInt(S, 8) 1
0123 10 unary + preferred 2
2345 10 unary + preferred 2
0x6b4 16 unary + preferred
6b4 16 use parseInt(S, 16)
>
>Notes :
>
>1: B is required for compatibility with ECMA 262 3rd Edn, and
for compatibility with all browsers.
>2: Common when converting the value of form controls to Number.
|
Looks good.
It needs to be compared with the FAQ note
<li><a href=
"http://www.jibbering.com/faq/faq_notes/type_convert.html#tcPrIntRx">
Javascript Type-Conversion - parseInt with a radix argument</a>
...
<FAQENTRY4.12 is
"The parseInt function decides what base the number is by looking at the
number. By convention it assumes any number beginning with 0 is Octal,
and any number beginning with 0x Hexadecimal. To force use of base 10
add a second parameter parseInt("09",10)"
and needs to be more like
"If no Base is given, the parseInt function decides what base the number
is in by looking at the number. It assumes that any number beginning
with 0x is Hexadecimal, and may assume that any number beginning with 0
is Octal. To force use of bases 8 or 10 add a second parameter, as in
parseInt("09", 10) or parseInt("077", 8).".
</FAQENTRY>
FAQ NOTES : type_convert
In table "Double NOT (!!col) : Other Values." and elsewhere, "return;"
serves no apparent purpose?
Just before heading "Converting to String", around "can avoid generating
errors" : IMHO it should note that, while no error will be raised by the
browser, an intention of the programmer may not be fulfilled and
alternative provision should be considered.
In "Converting to String", "the type-conversion mechanism is rarely
suited" - not so - not "rarely". It is suited to handling the results
of integer computation, and computation should be in integers where
practical (e.g. money).
In "Converting to Number", I would recommend, for safety and efficiency,
that the value of a numeric entry is generally converted to Number on
acquisition (rather than using repeated auto-conversion) :
Num = + form.control.value
or
Str = form.control.value
// Validate Str by RegExp
Num = + Str
"Strings that cannot be read as a number type-convert to NaN," - except
"Infinity", "+Infinity", "-Infinity" !
In "Parsing to Number", para 2 does not mention leading whitespace.
ISTM that it would be useful for each FAQ Note to contain a plaintext
date, altered at any significant change.
Something rather like our text above could be put into that section.
Inserted, with minor editing, in <URL: http://www.merlyn.demon.co.uk/js-
maths.htm>.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
September 1st, 2006 05:55 PM
# 11
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 8/30/2006 1:14 PM:
Quote:
Originally Posted by
JRS: In article <1156832149.528521.218420@74g2000cwt.googlegroups.c om>,
dated Mon, 28 Aug 2006 23:15:49 remote, seen in
news:comp.lang.javascript, RobG <rgqld@iinet.net.auposted :
Quote:
Originally Posted by
>Dr John Stockton wrote:
|
|
<snip>
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
>>For values of numeric properties, given in decimal without leading zero
>>and possibly followed by a unit (e.g. 33px), parseInt(S) is appropriate.
>>>
>>It is obvious that bases 2..7, 9, 11..15, 17..36 require parseInt(S, B).
|
>Probably not to most. Less condescending is:
>>
> "Bases 2..7, 9, 11..15, 17..36 require parseInt(S, B)."
|
>
But then someone like Randy will complain about 8 being missing, as
earlier in the thread. "... 17..36 clearly ..." ?
|
I asked about Base 8 for the same reason that Base 16 can be said to be
beneficial to require the Radix. If you compare the number of cases
where you can reliably omit the Radix compared to the number of cases
where you have to supply it, just going up to Base 16, it becomes
obvious - in a hurry - that always specifying it is the Best Practice.
As for omitting the Base with Base 8, you have a 1 in 8 chance of
getting it right.
<snip>
Quote:
Originally Posted by
<FAQENTRY4.12 is
"The parseInt function decides what base the number is by looking at the
number. By convention it assumes any number beginning with 0 is Octal,
and any number beginning with 0x Hexadecimal. To force use of base 10
add a second parameter parseInt("09",10)"
>
and needs to be more like
>
"If no Base is given, the parseInt function decides what base the number
is in by looking at the number. It assumes that any number beginning
with 0x is Hexadecimal, and may assume that any number beginning with 0
is Octal. To force use of bases 8 or 10 add a second parameter, as in
parseInt("09", 10) or parseInt("077", 8).".
</FAQENTRY>
|
<FAQENTRY>
"If no Base is given, the parseInt function decides what base the number
is in by looking at the number. It assumes that any number beginning
with 0x is Hexadecimal, and may assume that any number beginning with 0
is Octal. To remove this ambiguity, always use the Radix parameter with
parseInt".
</FAQENTRY>
Don't make it harder than it has to be in a FAQ Entry. In the Notes,
maybe, but anybody with a desire to understand parseInt can master it in
under 10 minutes.
<snip>
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
September 1st, 2006 10:35 PM
# 12
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <_O-dnTujw4yz62XZnZ2dnUVZ_vadnZ2d@comcast.com>, dated
Fri, 1 Sep 2006 14:05:30 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
><FAQENTRY>
>
>"If no Base is given, the parseInt function decides what base the number
>is in by looking at the number. It assumes that any number beginning
>with 0x is Hexadecimal, and may assume that any number beginning with 0
>is Octal. To remove this ambiguity, always use the Radix parameter with
>parseInt".
></FAQENTRY>
|
Naive. There are circumstances in which it is right for the user to
choose the base from 8, 10, 16. Don't be a Stalinist.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
September 2nd, 2006 02:55 AM
# 13
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 9/1/2006 4:06 PM:
Quote:
Originally Posted by
JRS: In article <_O-dnTujw4yz62XZnZ2dnUVZ_vadnZ2d@comcast.com>, dated
Fri, 1 Sep 2006 14:05:30 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
>
Quote:
Originally Posted by
><FAQENTRY>
>>
>"If no Base is given, the parseInt function decides what base the number
>is in by looking at the number. It assumes that any number beginning
>with 0x is Hexadecimal, and may assume that any number beginning with 0
>is Octal. To remove this ambiguity, always use the Radix parameter with
>parseInt".
></FAQENTRY>
|
>
>
Naive.
|
No, what is naive is your belief that parseInt is that difficult and/or
complicated. It isn't.
Quote:
Originally Posted by
There are circumstances in which it is right for the user to
choose the base from 8, 10, 16.
|
There is a need to *always* choose the base for 8 and 10.
Base 8:
parseInt('09') in Opera 9 gives 9. In IE and Mozilla it gives 0. That
alone makes it unreliable on the web to use parseInt for Base 8 without
the Radix.
Base 10:
Again, parseInt('09') gives different results in Opera 9 and other
browsers. That makes it unreliable for web use without the Radix for
Base 10.
That limits your statement to Base 16.
Which means that the *only* time you can *reliably* omit the Radix is
Base 16. And assuming that you are only dealing with Base 2-36 that is
1/35 times that it is reliable. 3%, for me, is not what I would term
"reliable". Nor is it worth the effort to remember it. Use the Radix and
you never have to worry with it. Besides, its about as much typing one
way as the other, 1 character difference:
parseInt('0bcd',16)
parseInt('0x0bcd')
1 Character. Yeah, that's a big savings. If you forget that 0x? It gives
0 and you are left scratching your head wondering why.
Yet you call that reasoning "Naive"?
Sidenote: The URL referenced in the FAQ:
<URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthparseint.asp>
Redirects to:
<URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/e86471af-2a0e-4359-83af-f1ac81e51421.asp>
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
September 2nd, 2006 10:25 PM
# 14
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <K9udnWZ_xYY8aWXZnZ2dnUVZ_oednZ2d@comcast.com>, dated
Fri, 1 Sep 2006 23:04:09 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>
Quote:
Originally Posted by
>There are circumstances in which it is right for the user to
>choose the base from 8, 10, 16.
|
>
>There is a need to *always* choose the base for 8 and 10.
>
>Base 8:
>parseInt('09') in Opera 9 gives 9. In IE and Mozilla it gives 0. That
>alone makes it unreliable on the web to use parseInt for Base 8 without
>the Radix.
|
Having been given an instruction that Hex can be written as 0xfff and
octal as 0777 otherwise decimal, anyone entering "09" deserves whatever
they get.
Quote:
Originally Posted by
>Base 10:
>Again, parseInt('09') gives different results in Opera 9 and other
>browsers. That makes it unreliable for web use without the Radix for
>Base 10.
|
No point in repeating that : parseInt("09") in any browser gives a
result independent of what base the user is hoping for.
Perhaps you have never heard of something called "feature detection"?
The programmer can test parseInt("09") and/or parseInt("077"), and adapt
the instructions seen by the user accordingly; if in his application it
is appropriate for the user to make the choice when typing in each
entry.
document.write("In this browser, 0... ",
parseInt("077")==63 ? "can" : "CANNOT",
" be used for Octal input. IAEFRTI.")
Quote:
Originally Posted by
>That limits your statement to Base 16.
>Which means that the *only* time you can *reliably* omit the Radix is
>Base 16. And assuming that you are only dealing with Base 2-36 that is
>1/35 times that it is reliable.
|
It is rare that all bases are equally likely, in my experience.
One can reliably omit the radix for decimal input if leading zeroes will
not be present, as explained previously. Function parseInt has uses
other than the digestion of strings typed by the user.
Quote:
Originally Posted by
3%, for me, is not what I would term
>"reliable". Nor is it worth the effort to remember it. Use the Radix and
>you never have to worry with it. Besides, its about as much typing one
>way as the other, 1 character difference:
>
>parseInt('0bcd',16)
>parseInt('0x0bcd')
|
In the code itself, both of those should be replaced by just 0x0bcd .
But if the average user of the page wishes to choose between Decimal and
Hexadecimal. he will not be able to do so by choosing the second
parameter in that simple fashion. He would need, say, radio-buttons or
another field to choose the base.
You need to distinguish more carefully between what you as a coder can
do and what anyone who may be reading your pages can do.
I trust that you will study the sig of this message.
--
It's a good idea to read the newsgroup and its FAQ.
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
|
|
September 4th, 2006 09:45 AM
# 15
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 9/2/2006 5:15 PM:
Quote:
Originally Posted by
JRS: In article <K9udnWZ_xYY8aWXZnZ2dnUVZ_oednZ2d@comcast.com>, dated
Fri, 1 Sep 2006 23:04:09 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
Quote:
Originally Posted by
>>There are circumstances in which it is right for the user to
>>choose the base from 8, 10, 16.
|
>There is a need to *always* choose the base for 8 and 10.
>>
>Base 8:
>parseInt('09') in Opera 9 gives 9. In IE and Mozilla it gives 0. That
>alone makes it unreliable on the web to use parseInt for Base 8 without
>the Radix.
|
>
Having been given an instruction that Hex can be written as 0xfff and
octal as 0777 otherwise decimal, anyone entering "09" deserves whatever
they get.
|
Your imagination amuses me sometimes. If that were true, then there
would be *NO* need for any kind of data validation at all. And to simply
say "It's the users fault because I, the programmer, don't want to use a
Radix", isn't the user getting what they deserve, it is ignorance on the
part of the programmer.
Quote:
Originally Posted by
Quote:
Originally Posted by
>Base 10:
>Again, parseInt('09') gives different results in Opera 9 and other
>browsers. That makes it unreliable for web use without the Radix for
>Base 10.
|
>
No point in repeating that : parseInt("09") in any browser gives a
result independent of what base the user is hoping for.
|
With you, you can never tell what needs to be repeated and what doesn't.
Quote:
Originally Posted by
Perhaps you have never heard of something called "feature detection"?
|
Are you kidding me? Write a lot of code to detect how parseInt works
when you can add 2, maybe 3, characters and have no problems at all?
That isn't "feature detection", that is ignorance based on writing code
(in your words here) "by the yard".
Quote:
Originally Posted by
The programmer can test parseInt("09") and/or parseInt("077"), and adapt
the instructions seen by the user accordingly; if in his application it
is appropriate for the user to make the choice when typing in each
entry.
|
There is no need for any of that and it is nothing more than an Academic
Exercise. You add the Radix and you don't have that issue to even consider.
Quote:
Originally Posted by
document.write("In this browser, 0... ",
parseInt("077")==63 ? "can" : "CANNOT",
" be used for Octal input. IAEFRTI.")
|
That is a joke isn't it?
Quote:
Originally Posted by
Quote:
Originally Posted by
>That limits your statement to Base 16.
>Which means that the *only* time you can *reliably* omit the Radix is
>Base 16. And assuming that you are only dealing with Base 2-36 that is
>1/35 times that it is reliable.
|
>
It is rare that all bases are equally likely, in my experience.
|
You are the one that brought up 2-36, not me. But that is your typical
style is to avoid your mistakes.
Quote:
Originally Posted by
One can reliably omit the radix for decimal input if leading zeroes will
not be present, as explained previously. Function parseInt has uses
other than the digestion of strings typed by the user.
|
Provide the Radix and it is *NEVER* an issue.
Quote:
Originally Posted by
Quote:
Originally Posted by
>3%, for me, is not what I would term
>"reliable". Nor is it worth the effort to remember it. Use the Radix and
>you never have to worry with it. Besides, its about as much typing one
>way as the other, 1 character difference:
>>
>parseInt('0bcd',16)
>parseInt('0x0bcd')
|
>
In the code itself, both of those should be replaced by just 0x0bcd .
|
OK, just for you, let me give you an example that just might satisfy
your pedantics.
Assume that your data is coming from a user entered field:
var inputValue = document.someForm.someInput.value;
Where the user enters the data.
parseInt(inputValue,16);
Now, will *THAT* satisfy your pedantic stupid arguments?
And don't go back to the "educate your users" hogwash argument.
Quote:
Originally Posted by
But if the average user of the page wishes to choose between Decimal and
Hexadecimal. he will not be able to do so by choosing the second
parameter in that simple fashion. He would need, say, radio-buttons or
another field to choose the base.
|
Aside from your grammatical errors, you don't say?
Quote:
Originally Posted by
You need to distinguish more carefully between what you as a coder can
do and what anyone who may be reading your pages can do.
|
And you need to distinguish more carefully between your pedantic pride
not allowing you to admit when you are wrong and plain common sense.
Stop making it harder than it has to be.
Quote:
Originally Posted by
I trust that you will study the sig of this message.
|
If there were anything in your signature worth studying then I might.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
September 4th, 2006 10:25 PM
# 16
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <4vidnYgEf_cWZWbZnZ2dnUVZ_q-dnZ2d@comcast.com>, dated
Mon, 4 Sep 2006 05:58:44 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>Dr John Stockton said the following on 9/2/2006 5:15 PM:
Quote:
Originally Posted by
>JRS: In article <K9udnWZ_xYY8aWXZnZ2dnUVZ_oednZ2d@comcast.com>, dated
>Fri, 1 Sep 2006 23:04:09 remote, seen in news:comp.lang.javascript,
>Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>>>There are circumstances in which it is right for the user to
>>>choose the base from 8, 10, 16.
>>There is a need to *always* choose the base for 8 and 10.
|
|
|
But you would enforce a base, rather than allowing a choice.
Quote:
Originally Posted by
Quote:
Originally Posted by
>Having been given an instruction that Hex can be written as 0xfff and
>octal as 0777 otherwise decimal, anyone entering "09" deserves whatever
>they get.
|
>
>Your imagination amuses me sometimes. If that were true, then there
>would be *NO* need for any kind of data validation at all.
|
The existence of at least one case where data validation is not needed
does not disprove the existence of cases where data validation is
appropriate. You've been following Logic 101 again, and that is known
to be buggy.
Quote:
Originally Posted by
>With you, you can never tell what needs to be repeated and what doesn't.
>
Quote:
Originally Posted by
>Perhaps you have never heard of something called "feature detection"?
|
>
>Are you kidding me? Write a lot of code to detect how parseInt works
>when you can add 2, maybe 3, characters and have no problems at all?
>That isn't "feature detection", that is ignorance based on writing code
>(in your words here) "by the yard".
|
With two, three, or even as many as four characters, how would you, as a
mere coder, allow the user to choose whether he wants to enter data in
decimal or hexadecimal?
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
>>That limits your statement to Base 16.
>>Which means that the *only* time you can *reliably* omit the Radix is
>>Base 16. And assuming that you are only dealing with Base 2-36 that is
>>1/35 times that it is reliable.
|
>>
>It is rare that all bases are equally likely, in my experience.
|
>
>You are the one that brought up 2-36, not me. But that is your typical
>style is to avoid your mistakes.
|
To include the possibility of bases 2 to 36 is by no means to say that
all are equally likely. When did you last use decimal? When did you
last use base 29?
Quote:
Originally Posted by
>Provide the Radix and it is *NEVER* an issue.
|
You, the coder, cannot know the preference of the user in all cases.
Quote:
Originally Posted by
>OK, just for you, let me give you an example that just might satisfy
>your pedantics.
>
>Assume that your data is coming from a user entered field:
>
>var inputValue = document.someForm.someInput.value;
>
>Where the user enters the data.
>
>parseInt(inputValue,16);
>
>Now, will *THAT* satisfy your pedantic stupid arguments?
|
That forces base 16, as you should know. It does not allow the user to
choose between decimal and hexadecimal. But parseInt(inputValue) does
that.
Quote:
Originally Posted by
Quote:
Originally Posted by
>But if the average user of the page wishes to choose between Decimal and
>Hexadecimal. he will not be able to do so by choosing the second
>parameter in that simple fashion. He would need, say, radio-buttons or
>another field to choose the base.
|
>
>Aside from your grammatical errors, you don't say?
|
Now we see that you do not understand the difference between a
grammatical error and a minor typo.
Quote:
Originally Posted by
Quote:
Originally Posted by
>I trust that you will study the sig of this message.
|
>
>If there were anything in your signature worth studying then I might.
|
Without studying it, how could you possibly know? - except for the line
corresponding to one in your own signature.
It's a good idea to read the newsgroup and its FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
September 12th, 2006 02:35 AM
# 17
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 9/4/2006 3:35 PM:
Quote:
Originally Posted by
JRS: In article <4vidnYgEf_cWZWbZnZ2dnUVZ_q-dnZ2d@comcast.com>, dated
Mon, 4 Sep 2006 05:58:44 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
|
I snipped everything in your post that was irrelevant to the FAQ Entry
that this thread was started on. If you want to give people the advice
to try to feature detect how to deal with Base 8, then please do. I will
reply back with how ignorant that idea is. Same for Base 10 and Base 16.
There is a very simple solution to it:
Always use the Radix and it will never matter.
I now return you all to your regularly scheduled CLJ noise of VK's posts.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
September 12th, 2006 10:35 PM
# 18
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <pqydnWlMK-rHg5vYnZ2dnUVZ_vKdnZ2d@comcast.com>, dated
Mon, 11 Sep 2006 22:41:48 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>Dr John Stockton said the following on 9/4/2006 3:35 PM:
Quote:
Originally Posted by
>JRS: In article <4vidnYgEf_cWZWbZnZ2dnUVZ_q-dnZ2d@comcast.com>, dated
>Mon, 4 Sep 2006 05:58:44 remote, seen in news:comp.lang.javascript,
>Randy Webb <HikksNotAtHome@aol.composted :
|
>
>I snipped everything in your post that was irrelevant to the FAQ Entry
>that this thread was started on. If you want to give people the advice
>to try to feature detect how to deal with Base 8, then please do. I will
>reply back with how ignorant that idea is. Same for Base 10 and Base 16.
>There is a very simple solution to it:
>
>Always use the Radix and it will never matter.
|
That's a control-freak policy, and if used by the coder it deprives the
user of the choice of radix between 8, 10, 16 (8 only in some browsers;
but that's a user choice).
Probably your experience is largely limited to the commercial world,
where the only numbers deal with amounts of money and quantities of
goods, both customarily represented in decimal. In the wider world
there are applications in which it would be appropriate to allow the
user to choose between decimal and hexadecimal directly, without
auxiliary controls.
A FAQ entry should not be unnecessarily restrictive.
It's a good idea to read the newsgroup and its FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<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.
|
|
September 14th, 2006 05:45 AM
# 19
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Dr John Stockton said the following on 9/12/2006 5:42 PM:
Quote:
Originally Posted by
JRS: In article <pqydnWlMK-rHg5vYnZ2dnUVZ_vKdnZ2d@comcast.com>, dated
Mon, 11 Sep 2006 22:41:48 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>Dr John Stockton said the following on 9/4/2006 3:35 PM:
Quote:
Originally Posted by
>>JRS: In article <4vidnYgEf_cWZWbZnZ2dnUVZ_q-dnZ2d@comcast.com>, dated
>>Mon, 4 Sep 2006 05:58:44 remote, seen in news:comp.lang.javascript,
>>Randy Webb <HikksNotAtHome@aol.composted :
|
>I snipped everything in your post that was irrelevant to the FAQ Entry
>that this thread was started on. If you want to give people the advice
>to try to feature detect how to deal with Base 8, then please do. I will
>reply back with how ignorant that idea is. Same for Base 10 and Base 16.
>There is a very simple solution to it:
>>
>Always use the Radix and it will never matter.
|
>
That's a control-freak policy,
|
No, it's a fool-proof code policy.
Quote:
Originally Posted by
and if used by the coder it deprives the user of the choice of radix
between 8, 10, 16 (8 only in some browsers; but that's a user choice).
|
No, the coder still has the ability to give the user that choice.
Quote:
Originally Posted by
Probably your experience is largely limited to the commercial world,
where the only numbers deal with amounts of money and quantities of
goods, both customarily represented in decimal.
|
Yes, it is largely limited to the commercial world although I have
volunteered to help with scripts in the scientific world where the Base
is not always 10.
Quote:
Originally Posted by
In the wider world there are applications in which it would be
appropriate to allow the user to choose between decimal and hexadecimal
directly, without auxiliary controls.
|
Example of that scenario?
Quote:
Originally Posted by
A FAQ entry should not be unnecessarily restrictive.
|
It should also promote the best practice and not specifying the Radix is
a recipe for disaster for 99% of the people who would be looking that up
in the FAQ.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
|
|
September 14th, 2006 03:05 PM
# 20
|
Re: FAQ Topic - Why does parseInt('09') give an error?
Randy Webb wrote:
(VK highlights with **)
Quote:
Originally Posted by
<FAQENTRY>
>
"If no *base encoding* is given, the parseInt function decides what base the number
is in by looking at the number. It assumes that any number beginning
with 0x is Hexadecimal, and may assume that any number beginning with 0
is Octal. To remove this ambiguity, always use the Radix parameter with
parseInt".
</FAQENTRY>
|
Perfect.
As OT in FAQ comments it is possible to discuss the IQ level of that
C'er who decided to prefix octals with 0 (zero) and hexs with 0x
instead of say o77 and hFF, but it will not change the standards so now
useless
Quote:
Originally Posted by
Don't make it harder than it has to be in a FAQ Entry. In the Notes,
maybe, but anybody with a desire to understand parseInt can master it in
under 10 minutes.
|
Full ACK
|
|
September 14th, 2006 11:05 PM
# 21
|
Re: FAQ Topic - Why does parseInt('09') give an error?
JRS: In article <yaGdnTuW1bhscpXYnZ2dnUVZ_vednZ2d@comcast.com>, dated
Thu, 14 Sep 2006 02:03:43 remote, seen in news:comp.lang.javascript,
Randy Webb <HikksNotAtHome@aol.composted :
Quote:
Originally Posted by
>
>It should also promote the best practice and not specifying the Radix is
>a recipe for disaster for 99% of the people who would be looking that up
>in the FAQ.
|
Control freak. 99% != 100%.
In helping the multitude, it is wrong to mislead the intelligent
minority.
--
© John Stockton, Surrey, UK. ???@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.
|