[thelist] parseInt in javascript

MRC webmaster at equilon-mrc.com
Mon Dec 24 09:55:52 CST 2001


.jeff and Mark,

> > Be careful using parseInt() here.  If the string starts with a 0 it
> > will be parsed as an octal number.
>
> I've had a very odd result when parsing some numbers using parseInt, in my
> case for checking whether a date was valid. I found that if you parseInt
> "08" or "09" (strings from a form field), you get 0. (Other numbers from 1
> to 100 seem to be OK; This is on Mac, if that makes a difference).

    Of course the solution to this is to use a radix value in your parseInt
method:

var strNumber = "08" ;
var intNumber = parseInt(strNumber, 10) ;

    Without the radix, you'll get the octal result of 0; with the radix
value of 10, you get the integer result of 8.

James Aylard





More information about the thelist mailing list