Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Web Development > HTML/DHTML - JavaScript/VBScript

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

JavaScript parseInt() bug

By pradeep pradeep is offline

On 2nd June, 2006
Exclamation JavaScript parseInt() bug

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy JavaScript parseInt() bug link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

There is a "bug" with the parseInt JavaScript function. The bug is not something that will affect you very often, but it is something you should be aware of. We've seen the bug in every browser except Opera.

I've created a button to demonstrate the bug. The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The button below builds statements from parseInt("01") through parseInt("09") and shows what the resulting value is. But it also does parseFloat("01") through parseFloat("09"). This shows that the bug does not exist with parseFloat.

Keep in mind that this bug only happens when the value being checked is a string and only when the string starts with a leading zero. So that's why it is difficult to notice. But if you're dealing with a web page that has user input, there's nothing prevening the user from entering 08 for a number field. To be 100% confident that you won't see the bug, use one of these two techniques:

Code: JavaScript
parseInt(parseFloat(<my text value>))
 
 parseInt(<my text value>, 10)
The "10" in the second example tells the browser that base-10 values should be used. Here is the button so you can see what's happening. The button puts up an alert box with the result of the "buildString" function, which is shown below:

Code: JavaScript
function buildString() {
    var ret = "";
    for (var i=1; i<=9; i++) {
       // Build a statement like parseInt("0?") where ? varies from "1" to "9"
       fn = "parseInt(\"0" + i + "\")";
       // Evaluate the statement to get the result
       ret += fn + " = " + eval(fn) + "\n";
       // Do the same thing, except with parseFloat instead of parseInt
       fn = "parseFloat(\"0" + i + "\")";
       ret += fn + " = " + eval(fn) + "\n";
       // This time do parseInt but specify base 10.
       fn = "parseInt(\"0" + i + "\", 10)";
       ret += fn + " = " + eval(fn) + "\n\n";
    }
    return ret;
 }
Old 08-30-2007, 07:19 AM   #2
yayak
Newbie Member
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
yayak is on a distinguished road

Re: JavaScript parseInt() bug


there are other techniques, try these:
Code:
parseInt(<my text value>.replace(/^0+/g, ''));
   ex.: parseInt("08".replace(/^0+/g, ''));
parseInt(<my text value> * 1);
   ex.: parseInt("08" * 1);
yayak is offline   Reply With Quote
Old 08-30-2007, 11:01 AM   #3
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: JavaScript parseInt() bug


Good, but the techniques mentioned above are technically correct.
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Form validation with PHP & JavaScript pradeep PHP 18 09-08-2009 01:49 AM
Few basic JavaScript questions pradeep HTML/DHTML/CSS -JavaScript/VBScript 2 07-21-2007 08:48 PM
Extending JavaScript Arrays pradeep HTML/DHTML - JavaScript/VBScript 0 02-27-2006 07:11 PM
JavaScript Triggers pradeep HTML/DHTML - JavaScript/VBScript 0 10-01-2005 06:06 PM
Microsoft fixes big browser bug shabbir Information Technology 0 08-03-2004 10:44 AM

 

All times are GMT +5.5. The time now is 05:56 AM.