Prev: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-07-28)
Next: ANUSHKA HOT PICTURES FOR BOLLYWOOD FANS
From: FAQ server on 29 Jul 2010 19:00 ----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- 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` may be 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)`. <URL: http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/parseInt> <URL: http://msdn.microsoft.com/en-us/library/x53yedee%28VS.85%29.aspx> <URL: http://docs.sun.com/source/816-6408-10/toplev.htm#1064173> <URL: http://jibbering.com/faq/notes/type-conversion/#tcPrIntRx> The complete comp.lang.javascript FAQ is at http://jibbering.com/faq/ -- The sendings of these daily posts are proficiently hosted by http://www.pair.com.
From: Dr J R Stockton on 31 Jul 2010 18:02 In comp.lang.javascript message <4c5207fc$0$280$14726298(a)news.sunsite.dk >, Thu, 29 Jul 2010 23:00:03, FAQ server <javascript(a)dotinternet.be> posted: > >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` may be >parsed as octal (octal digits are `0-7`). The string `'09'` >is converted to `0`. > The string `'09'` is converted to `0`. Not always. So it should be The string `'09'` is then converted to `0`. Opera gives 9. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
From: Captain Paralytic on 1 Aug 2010 19:01 On Jul 31, 11:02 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> wrote: > In comp.lang.javascript message <4c5207fc$0$280$14726...(a)news.sunsite.dk>, Thu, 29 Jul 2010 23:00:03, FAQ server <javascr...(a)dotinternet.be> > > posted: > > > > >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` may be > >parsed as octal (octal digits are `0-7`). The string `'09'` > >is converted to `0`. > > The string `'09'` is converted to `0`. > > Not always. So it should be > The string `'09'` is then converted to `0`. > > Opera gives 9. But the subject of this entry is "Why does K = parseInt('09') set K to 0?" In the cases that match the subject (i.e. that K is set to 0), the string is ALWAYS converted to 0. If as you suggest the string in a particular case was converted to 9, the answer would not match the question in this case.
From: Dr J R Stockton on 3 Aug 2010 13:04
In comp.lang.javascript message <5777d821-217f-4c29-be59-7aa0b0bcbc31(a)f2 0g2000pro.googlegroups.com>, Sun, 1 Aug 2010 16:01:53, Captain Paralytic <paul_lautman(a)yahoo.com> posted: >On Jul 31, 11:02�pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> >wrote: >> In comp.lang.javascript message <4c5207fc$0$280$14726...(a)news.sunsite >>.dk>, Thu, 29 Jul 2010 23:00:03, FAQ server >><javascr...(a)dotinternet.be> >> >> posted: >> >> >> >> >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` may be >> >parsed as octal (octal digits are `0-7`). The string `'09'` >> >is converted to `0`. >> > The string `'09'` is converted to `0`. >> >> Not always. �So it should be >> � The string `'09'` is then converted to `0`. >> >> Opera gives 9. >But the subject of this entry is "Why does K = parseInt('09') set K to >0?" > >In the cases that match the subject (i.e. that K is set to 0), the >string is ALWAYS converted to 0. If as you suggest the string in a >particular case was converted to 9, the answer would not match the >question in this case. The Subject does not say "always". Where a FAQ section Subject is a question, the section content should AT LEAST answer the question as asked, and should say as much more as is relevant and reasonable. In this case, the applicable standard id ECMA 262 5th Edn, which includes "15.1.2.2: The specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values.". One well-known browser already complies. A user asking the Subject question may subsequently find that it no longer happens. Therefore, the FAQ answer should at least mention the possibility of standards-compliant behaviour. -- (c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 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. |