473,657 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parseInt() question

Why does parseInt("00000 00000000018") return 1, while
parseInt("00000 00000000018", 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 1794
wrote on 17 jun 2007 in comp.lang.javas cript:
Why does parseInt("00000 00000000018") return 1, while
parseInt("00000 00000000018", 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(numStr ing, [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("00000 00000000018") return 1, while
parseInt("00000 00000000018", 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.javas cript:
>Why does parseInt("00000 00000000018") return 1, while
parseInt("0000 000000000018", 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(numStr ing, [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.javas cript:
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.javas cript:
>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.javas cript:
> 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.javas cript:
>> 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('0001 81111') will also - rightfully - give 1). Then it
converts 0001 in Base 8, which is 1.

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

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '07 #8
In comp.lang.javas cript message <11************ **********@q69g 2000hsb.go
oglegroups.com> , Sun, 17 Jun 2007 09:55:37, ki*******@gmail .com posted:
>Why does parseInt("00000 00000000018") return 1, while
parseInt("0000 000000000018", 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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 18 '07 #9
In comp.lang.javas cript message <E9CdnZGwH5q57e jbnZ2dnUVZ_tKjn Z2d@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.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demo n.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

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

Similar topics

5
3512
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
54172
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
3267
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 this without writing a routine to check for the zero first? Thanks,
6
8764
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 Math.ceil/floor and parseInt +/- 1 to ensure the amount to move was at least 1 and in the right direction. I made a small test to see which one is faster and also included simply adding/subtracting 1. parseInt generally took 50% to 100% longer than...
25
9574
by: FAQ server | last post 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...
14
2963
by: FAQ server | last post by:
----------------------------------------------------------------------- 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...
6
1495
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 that simply doing the following , tempx = parseInt(((fpssArray).toString()).substring((m*3), (m*3)+3)); does NOT work.
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.