Why javascript parseInt ignoring leading zeros in the string? -
this question has answer here:
i trying convert string number, "09"
parseint('09') //returns 9
what best way number leading 0 , better if explain, why designed parseint in such way doesn't care leading zeros.
an int
cannot have leading zeroes, since don't have mathematical meaning.
to store number, languages use binary representation (don't forget 0s , 1s in end). representation same 9, 09, or 00009. when number must printed, converted string representation, lose leading zeroes.
if need store/remember 0s, choice store string representation of number.
what storing both number , string representation, this:
function myint(s){ this.asstring = s; this.num = parseint(s); } var = new myint("09"); console.log(i.num); // 9 console.log(i.asstring); // 09
take note leading 0 don't have value int. if want use display purpose use string , calculation part use parseint(someval)
Comments
Post a Comment