Javascript String to Int April 4, 2006 by Tom 12 Comments Handy function: var yourInt; yourInt = parseInt(str); More Info: http://www.devguru.com/Technologies/ecmascript/quickref/parseint.html
dav7 says May 20, 2007 at 2:05 am Wow, thanks for that. First hit in Google and exact result to my problem (“start = parseInt(document.getElementById(‘text’).value);”). My function was returning an array with missing values when I tried to access `start – 1′ but as soon as I parseInt’ed it all was well \o/ 😀 -dav7
Scott says May 20, 2007 at 7:15 pm Perfect, thanks! I was lamenting having to use eval(), so this is perfect!
Airor says October 5, 2007 at 10:31 am Instead I use the built in typecasting myInt = 0 + str; myStr = ” + i + ‘ number of things.’;
johnsmith says July 10, 2008 at 1:36 pm Ja pierdole ale niemoc pisać tak zjebane artykuły. Nice 🙂
Barnaby says September 13, 2008 at 8:28 am haha, to jest całkiem bez sensu artykuł Pretty cool, thanks 🙂
Luong Xuan Thuy says September 26, 2008 at 2:40 am ———————————– var yourInt; yourInt = parseInt(str); ———————————– You can not use this code fragment if your string is “009”. parseInt(“099”)=0; Because “000” not is a octal digits. Thanks.
Luong Xuan Thuy says September 26, 2008 at 2:40 am Sorry, I mean : Because “099″ not is a octal digits.
Istvan says October 23, 2008 at 4:45 am You should always explicitly specify the radix to be safe: var yourInt; yourInt = parseInt(str,10); In this case you won’t have problems with strings that can be interpreted as octal numbers. From the javascript specification: If the radix parameter is omitted, JavaScript assumes the following: If the string begins with “0x”, the radix is 16 (hexadecimal) If the string begins with “0”, the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal) Not all browsers implement the specification in the same way, so if you want your code to work consistently everywhere you shouldn’t omit the radix.
brandonh says November 22, 2008 at 2:56 pm thank you so much lstvan. i was kicking myself trying to see why 08 was returning 0. you rule.
I&Mdesign says December 4, 2008 at 9:34 am Thanks a lot for your explanation about hexadecimal and octal numbers espesially. It works!
Wow, thanks for that. First hit in Google and exact result to my problem (“start = parseInt(document.getElementById(‘text’).value);”).
My function was returning an array with missing values when I tried to access `start – 1′ but as soon as I parseInt’ed it all was well \o/ 😀
-dav7
Perfect, thanks! I was lamenting having to use eval(), so this is perfect!
Instead I use the built in typecasting
myInt = 0 + str;
myStr = ” + i + ‘ number of things.’;
Nice one,
tkx
Ja pierdole ale niemoc pisać tak zjebane artykuły.
Nice 🙂
thanks!
🙂
haha, to jest całkiem bez sensu artykuł
Pretty cool, thanks 🙂
———————————–
var yourInt;
yourInt = parseInt(str);
———————————–
You can not use this code fragment if your string is “009”. parseInt(“099”)=0;
Because “000” not is a octal digits.
Thanks.
Sorry, I mean : Because “099″ not is a octal digits.
You should always explicitly specify the radix to be safe:
var yourInt;
yourInt = parseInt(str,10);
In this case you won’t have problems with strings that can be interpreted as octal numbers.
From the javascript specification:
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with “0x”, the radix is 16 (hexadecimal)
If the string begins with “0”, the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
Not all browsers implement the specification in the same way, so if you want your code to work consistently everywhere you shouldn’t omit the radix.
thank you so much lstvan. i was kicking myself trying to see why 08 was returning 0. you rule.
Thanks a lot for your explanation about hexadecimal and octal numbers espesially. It works!