View Full Version : ParseInt leading 0 problem

02-05-2004, 06:03 PM
I am getting weird results with parseInt if the number has leading zeros

here is the test code
var frm = document.forms.inform;

var instr = frm.textfield.value;

var inint = parseInt(instr);

var outstr = inint + " ";

frm.textfield2.value = outstr;
frm.textfield3.value = inint;

input output

098 0
003 3
022 18
011 9
067 055

Apparently it is interpreting it as octal.

what iam after is 003 --> 3

045 ---> 45

02-05-2004, 06:38 PM
parseInt(sNumber, nRadix);

If you specify the radix to be 10, it will be interpreted as a decimal number instead of octal.

Roy Sinclair
02-05-2004, 08:13 PM
What you're seeing is the end result of a bad decision made early on in the development of the language. The decision was that the parseInt function would be able to automatically decide which "radix" to use for the input number based on the format of the input number unless you specified an explicit radix. You would be amazed at how many scripts have this little bug in them waiting to surface or already causing people to wonder why their numbers keep changing values.