473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

parseInt() question

Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn't you want to get back 18 in most if not all cases?

Any thoughts?

-Thx

Jun 17 '07 #1
12 1774
wrote on 17 jun 2007 in comp.lang.javascript:
Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix.
Your assumption is wrong, it is octal.
Read the specs:

parseInt(numString, [radix])

numString
Required. A string to convert into a number.

radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of '0x' are
considered hexadecimal and strings with a prefix of '0' are considered
octal. All other strings are considered decimal.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 17 '07 #2
ki*******@gmail.com wrote:
Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn't you want to get back 18 in most if not all cases?
There was a mistake made in the specification of parseInt. That is why you
should always explicitly indicate the radix. Don't depend on the default being
10. As you demonstrated, it is not reliable.

JSLint will read your source and identify the places where the default is missing.

http://www.JSLint.com/
Jun 17 '07 #3
Evertjan. wrote:
wrote on 17 jun 2007 in comp.lang.javascript:
>Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix.

Your assumption is wrong, it is octal.
Read the specs:

parseInt(numString, [radix])

numString
Required. A string to convert into a number.

radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of '0x' are
considered hexadecimal and strings with a prefix of '0' are considered
octal. All other strings are considered decimal.
I'd like to know where you read that from. The Core JavaScript 1.5
specifically states that, that behavior is deprecated.

http://developer.mozilla.org/en/docs...nt#Description

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 17 '07 #4
-Lost wrote on 17 jun 2007 in comp.lang.javascript:
Evertjan. wrote:
>radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of
'0x' are considered hexadecimal and strings with a prefix of '0' are
considered octal. All other strings are considered decimal.

I'd like to know where you read that from.
MS
The Core JavaScript 1.5
specifically states that, that behavior is deprecated.
Could be that the behavior is deprecated,
but it still seems to work that way.

<script type='text/javascript'>
alert(parseInt('00018')) // returns 1 in IE7 and FF2
</script>

Do you sometimes feel deprecated and Lost forever too,
dreadful sorry, Clementine?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 17 '07 #5
Evertjan. wrote:
-Lost wrote on 17 jun 2007 in comp.lang.javascript:
>Evertjan. wrote:
>>radix
Optional. A value between 2 and 36 indicating the base of the number
contained in numString. If not supplied, strings with a prefix of
'0x' are considered hexadecimal and strings with a prefix of '0' are
considered octal. All other strings are considered decimal.
I'd like to know where you read that from.

MS
Ah, OK.
>The Core JavaScript 1.5
specifically states that, that behavior is deprecated.

Could be that the behavior is deprecated,
but it still seems to work that way.

<script type='text/javascript'>
alert(parseInt('00018')) // returns 1 in IE7 and FF2
</script>
Right, I see that. Don't understand it, but I see it.
Do you sometimes feel deprecated and Lost forever too,
dreadful sorry, Clementine?
I am not sure I understand what you said, but yes, I am lost quite often
(front lobe disabilities affect problem solving). Anyway, I never fully
understand how parseInt works. I have to read it a thousand times
before realizing (for example) that:

parseInt('18' 8); should *not* return 22, but parseInt('22', 8); should
return 18.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 17 '07 #6
-Lost wrote on 17 jun 2007 in comp.lang.javascript:
> alert(parseInt('00018')) // returns 1 in IE7 and FF2

Right, I see that. Don't understand it, but I see it.
I think [but am not sure] it works this way:

0 octal assumed

00 skipped

1 value is one

8 value over 7 not part of octal number,
so 8 is considered to be a letter,
parsing ended.

result value is 1.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 17 '07 #7
Evertjan. said the following on 6/17/2007 2:10 PM:
-Lost wrote on 17 jun 2007 in comp.lang.javascript:
>> alert(parseInt('00018')) // returns 1 in IE7 and FF2
Right, I see that. Don't understand it, but I see it.

I think [but am not sure] it works this way:

0 octal assumed

00 skipped

1 value is one

8 value over 7 not part of octal number,
so 8 is considered to be a letter,
parsing ended.

result value is 1.
Read the string, from left to right, until you encounter a character
that is not in the base set. The string that you have read, up until
then, convert it to the base. So, it reads until it finds the 8, stops
reading (parseInt('000181111') will also - rightfully - give 1). Then it
converts 0001 in Base 8, which is 1.

parseInt('0001238') might show it a little easier to see.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '07 #8
In comp.lang.javascript message <11**********************@q69g2000hsb.go
oglegroups.com>, Sun, 17 Jun 2007 09:55:37, ki*******@gmail.com posted:
>Why does parseInt("0000000000000018") return 1, while
parseInt("0000000000000018", 10) return 18?

My assumption was that the base 10 would be default argument for
radix. Wouldn't you want to get back 18 in most if not all cases?

Any thoughts?
You should have read the newsgroup FAQ. One section fairly obviously
applies. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
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.
Jun 18 '07 #9
In comp.lang.javascript message <E9CdnZGwH5q57ejbnZ2dnUVZ_tKjnZ2d@comcas
t.com>, Sun, 17 Jun 2007 13:33:54, -Lost <ma****************@techie.com>
posted:
>
I'd like to know where you read that from. The Core JavaScript 1.5
specifically states that, that behavior is deprecated.
If something is currently deprecated, it can be assumed to exist.

However, it should not be used if there is a non-deprecated alternative,
and it should not be relied upon.

OTOH, when code is being read, it is well to be able to understand the
deprecated construct.

The FAQ refers.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 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.
Jun 18 '07 #10
dd
On Jun 17, 7:33 pm, -Lost <maventheextrawo...@techie.comwrote:
The Core JavaScript 1.5 specifically states
that, that behavior is deprecated.
JavaScript deprecation rules seem to be following Java.
When they say something is deprecated, what they mean is
that this method/property is no longer recommended and
in future maybe removed from the language. So you really
ought not to be using deprecated features because they
may just disappear one day, however, they are still there
right now and will still work.

The big difference between Java and JavaScript though
when it comes to deprecation is that there's no
compiler involved with JavaScript. This means that
there are many millions of web pages out there
that might break if deprecated features were actually
removed from the language. So it's probably not going
to happen.

Only Microsoft would do such a thing (re: EOLAS issue).

In the Java world, they can stop supporting deprecated
features in the compilers (but not in the JVMs) and it
can force people to stop using the feature. I don't
recall any specific example of Sun doing this though.

Jun 19 '07 #11
dd wrote:
On Jun 17, 7:33 pm, -Lost <maventheextrawo...@techie.comwrote:
>The Core JavaScript 1.5 specifically states
that, that behavior is deprecated.

JavaScript deprecation rules seem to be following Java.
When they say something is deprecated, what they mean is
that this method/property is no longer recommended and
in future maybe removed from the language. So you really
ought not to be using deprecated features because they
may just disappear one day, however, they are still there
right now and will still work.

The big difference between Java and JavaScript though
when it comes to deprecation is that there's no
compiler involved with JavaScript. This means that
there are many millions of web pages out there
that might break if deprecated features were actually
removed from the language. So it's probably not going
to happen.

Only Microsoft would do such a thing (re: EOLAS issue).

In the Java world, they can stop supporting deprecated
features in the compilers (but not in the JVMs) and it
can force people to stop using the feature. I don't
recall any specific example of Sun doing this though.
Just for clarity's sake (well, mainly for me).

If browser vendors were to update their JavaScript engines to reflect a
deprecated feature, that would then break JavaScript applications.

With a Java .class or .jar or whatnot (or whatever the compiled code may
become), it would not break if the JVMs were updated to reflect
deprecated features. Right?

I assume this because the Java code is a compiled version, where the
JavaScript code is "compiled" every time it is accessed.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 19 '07 #12
-Lost said the following on 6/19/2007 9:25 AM:
dd wrote:
>On Jun 17, 7:33 pm, -Lost <maventheextrawo...@techie.comwrote:
>>The Core JavaScript 1.5 specifically states
that, that behavior is deprecated.

JavaScript deprecation rules seem to be following Java.
When they say something is deprecated, what they mean is
that this method/property is no longer recommended and
in future maybe removed from the language. So you really
ought not to be using deprecated features because they
may just disappear one day, however, they are still there
right now and will still work.

The big difference between Java and JavaScript though
when it comes to deprecation is that there's no
compiler involved with JavaScript. This means that
there are many millions of web pages out there
that might break if deprecated features were actually
removed from the language. So it's probably not going
to happen.

Only Microsoft would do such a thing (re: EOLAS issue).

In the Java world, they can stop supporting deprecated
features in the compilers (but not in the JVMs) and it
can force people to stop using the feature. I don't
recall any specific example of Sun doing this though.

Just for clarity's sake (well, mainly for me).

If browser vendors were to update their JavaScript engines to reflect a
deprecated feature, that would then break JavaScript applications.

With a Java .class or .jar or whatnot (or whatever the compiled code may
become), it would not break if the JVMs were updated to reflect
deprecated features. Right?
No. It would break with a new engine if the new engine didn't support
deprecated features until the .class,.jar files were recompiled and
re-downloaded by the client.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 19 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Thies | last post by:
I've noticed that doing: var x=any number between -1 and 1, like 0.5 parsInt(x) yields NaN. I was expecting zero. Is there another way of doing this?
9
by: Henrik | last post by:
In Java you can write something like this. Does anyone know how to do this in javascript? "byte b=Integer.parseInt(int value or String).byteValue;"
14
by: jacster | last post by:
Hi, I'm trying to parse a string of the form 08:00 representing a time so I can calculate the difference between two times. parseInt(time) with a leading zero returns 0. Is there a way around...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
25
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does parseInt('09') give an error? ----------------------------------------------------------------------- ...
14
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- ...
6
by: lorlarz | last post by:
Regarding http://mynichecomputing.com/digitallearning/yourOwn.htm and how to make it fail when it should not with an integer OR parseInt to integer conversion problem. THE real problem IS is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.