464,673 Members | 1,267 Online
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

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

 P: n/a ----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- The parseInt function decides what base the number is by looking at the number. It assumes that any number beginning with '0x' or '0X' is hexadecimal, but it has a choice with a leading zero: the number can either be octal or decimal. Assuming octal, the string '09' will be converted to 0 (octal digits are 0-7); assuming decimal, '09' will be converted to 9 (the leading zero is ignored). To force use of a particular base, add a second parameter: « parseInt("09",base) » http://msdn.microsoft.com/library/en...thparseint.asp http://docs.sun.com/source/816-6408-...ev.htm#1064173 http://www.jibbering.com/faq/faq_not....html#FAQN4_12 === Postings such as this are automatically sent once a day. Their goal is to answer repeated questions, and to offer the content to the community for continuous evaluation/improvement. The complete comp.lang.javascript FAQ is at http://www.jibbering.com/faq/. The FAQ workers are a group of volunteers. Dec 19 '06 #1
14 Replies

 P: n/a FAQ server wrote: ------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ------------------------------------------------------- The parseInt function decides what base the number is by looking at the number. It assumes that any number beginning with '0x' or '0X' is hexadecimal, but it has a choice with a leading zero: the number can either be octal or decimal. Shouldn't at least the second and third occurrence of the word "number" in those sentences be "string", and possible all of the first three? The addition of an opening sentence stating that "the parseInt function takes sting input and parses it into numeric output" (or something like that) may make the following text easier to render accurate without its becoming obscure in the process. Richard. Dec 19 '06 #2

 P: n/a Richard Cornford said the following on 12/18/2006 7:50 PM: FAQ server wrote: >-------------------------------------------------------FAQ Topic - Why does K = parseInt('09') set K to 0?-------------------------------------------------------The parseInt function decides what base the number is bylooking at the number. It assumes that any number beginningwith '0x' or '0X' is hexadecimal, but it has a choice with aleading zero: the number can either be octal or decimal. Shouldn't at least the second and third occurrence of the word "number" in those sentences be "string", and possible all of the first three? Probably all three. The addition of an opening sentence stating that "the parseInt function takes sting input and parses it into numeric output" (or something like that) may make the following text easier to render accurate without its becoming obscure in the process. The parseInt function decides what base to convert a string to by looking at the string. It assumes that any string beginning with '0x' or '0X' represents a hexadecimal Number, but it has a choice with a leading zero: the string can represent a Number that can be either octal or decimal. Assuming octal, the string '09' will be converted to 0 (octal digits are 0-7); assuming decimal, '09' will be converted to 9 (the leading zero is ignored). To force use of a particular base, add a second parameter: parseInt("09",base). Sidenote: The MSDN URL for that article has been changed by MS. I guess I need to double check all of the MSDN URL's to make sure the links in the FAQ don't lead to a redirect page. Any idea why http://jibbering.com/faq/ gives the old version instead of the updated version? At least for me it does. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Dec 19 '06 #3

 P: n/a Randy Webb wrote: Hi May I hazard some amateur comments:- [snip] The parseInt function decides what base to convert a string to by looking at the string. Perhaps "...to convert a number represented in a string by...." >It assumes that any string beginning with '0x' or '0X' represents a hexadecimal Number, Should "Number" be in upper case? Might that confuse numeric literals, with JavaScript's internal Number data type? Perhaps "...indicates a number represented in hexadecimal..." >but it has a "...but, if an 'x' or 'X' is not present, has a..." choice with a leading zero: the string can represent a Number that can be either octal or decimal. "...could represent a number...." >Assuming octal, the string '09' "A leading '0' will cause octal will be the first assumption, otherwise decimal is assumed. As a result the string '09'...." will be converted to 0 (octal digits are 0-7); "....(valid octal digits are 0-7, so parseInt will stop parsing when it reaches '9')..." >assuming decimal, '09' will be converted to 9 (the leading zero is ignored). To force use of a particular base, add a second parameter: parseInt("09",base). "To force use of a particular base, add a second parameter: parseInt("09",base). If decimal is forced, '09' will be converted to 9 (the leading zero is ignored)." Regards Julian Dec 19 '06 #4

 P: n/a Randy Webb wrote: Richard Cornford said the following on 12/18/2006 7:50 PM: >FAQ server wrote: >>-------------------------------------------------------FAQ Topic - Why does K = parseInt('09') set K to 0?-------------------------------------------------------The parseInt function decides what base the number is bylooking at the number. It assumes that any number beginningwith '0x' or '0X' is hexadecimal, but it has a choice with aleading zero: the number can either be octal or decimal. Shouldn't at least the second and third occurrence of the word "number"in those sentences be "string", and possible all of the first three? The second, yes. The first and third might be reworded, but to replace either with "string" is incorrect. In both cases, it is the number represented within the string that's significant, something that I clearly failed to communicate. A string doesn't have to begin with '0x' or '0X' to be treated as hexadecimal; it does have '0x' or '0X' as the first non-white space characters. [snip] Any idea why http://jibbering.com/faq/ gives the old version instead of the updated version? At least for me it does. No, it's bizarre. Both /faq/index.html and /faq/index.asp lead to the updated version but none of the other typical directory index paths that I can think of refer to either FAQ version. Perhaps Jim or his host can shed some light on it. The FAQ is currently sent with a "Cache-Control: private" header name/value pair and no cache validators. It would nice if this were changed so visitors don't have to download the FAQ again unless it's been modified. The private Cache-Control directive doesn't necessarily restrict a user agent cache, but does unnecessarily prevent shared caches from storing the entity. Mike Dec 19 '06 #5

 P: n/a In comp.lang.javascript message <45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02, FAQ server -----------------------------------------------------------------------FAQ Topic - Why does K = parseInt('09') set K to 0?-----------------------------------------------------------------------The parseInt function decides what base the number is bylooking at the number. It assumes that any number beginningwith '0x' or '0X' is hexadecimal, but it has a choice with aleading zero: the number can either be octal or decimal.Assuming octal, the string '09' will be converted to 0 (octaldigits are 0-7); assuming decimal, '09' will be converted to 9(the leading zero is ignored). To force use of a particular base, add a second parameter: â€¹ parseInt("09",base) â€º The text does not really answer the Subject question. Function parseInt has two important features that other conversions lack : it has flexibility of base, AND, after the first digit, it treats the first character that is not a digit in the current base as a legitimate terminator (that may not be 100% accurate, but indicates the point). Contrast parseInt("6px") with Number("6px") and +"6px" . So put "Function parseInt reads a String and returns a corresponding Number; conversion of the digital part ends at any character which is not a digit in the current base. If parseInt('09') is considered octal, the base is 8, the 9 ends conversion, and Number 0 is returned. So parseInt('0449') would then return Number octal 44 = decimal 36." then more or less as before with 'string' wherever the input is being referred to. In parseInt(S, B) there remains the question of how B is handled : in IE6, parseInt("0449", 012) gives Number 449. In IE6, it seems that literals are treated as if by parseInt(S), with 012 being ten (ISTM that IE4 maybe took it as 12). That can be important in date work, where one may well be tempted to write Oct7th = new Date(Y, 09, 07) Nov7th = new Date(Y, 10, 07) for alignment, and I think FF2 may object to that. Then in dealing with Ordinal Date YYYY-DDD in IE6 a literal 044 is thirty-six but 088 is eighty-eight. So I suggest a final paragraph to that FAQ section - "A literal L in the source is read as if by parseInt(L), so 012 means ten." - or whatever similar covers all cases. A better Subject might be "Why is my 09 read as Zero?" - it accommodates more. Where a RegExp is used to split a string into decimal parts, ISTM that there could be advantage in expressing a part not as (\d+) but as 0*(\d+) thereby excluding leading zeroes. I've not yet seen a need to do so, but ... . -- (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web

 P: n/a Dr J R Stockton said the following on 12/20/2006 6:40 PM: In comp.lang.javascript message , Tue, 19 Dec 2006 21:17:17, Randy Webb Dr J R Stockton said the following on 12/19/2006 5:18 PM: >>In comp.lang.javascript message<45***********************@news.sunsite.dk>, Tue, 19 Dec 2006 00:00:02,FAQ server >But that issue seems to be Date related, not parseInt related or doesthe Date Object use parseInt to read its parameters? >>So I suggest a final paragraph to that FAQ section -"A literal L in the source is read as if by parseInt(L), so 012 meansten." - or whatever similar covers all cases. "The same rules apply to the Base parameter as apply to the firstparameter" or similar? NO. ICBW, but ISTM that a literal base parameter is treated as any other literal. Therefore, it would be wrong to make a remark specifically about the base parameter. Then there will be no remark at all. >>A better Subject might be "Why is my 09 read as Zero?" - it accommodatesmore. But it doesn't cover - directly - the issue with using parseInt on"09" or the likes. Indeed it does cover it; though it does not cover 012 being read as ten. Why answer a narrow question when a broader one would, without needing to say much more, deal with the full range of similar cases? Subject "Why are Strings beginning with a zero digit sometimes read as unexpected integer Numbers?" Do you honestly believe that people looking for an answer to parseInt('09') being set to zero are going to look at a question phrased as you have it there? The FAQ is intended - or it should be - for people trying to learn from it, not from Anally Retentive know-it-alls that want a quick personal reference document. Unless there is some other problem with that entry (from someone besides you), it will stay worded the way it is - along with the question. I am not going to argue pedantic BS with you. -- Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Dec 21 '06 #11