Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
EXTREMELY confused here.
parseInt("09") = 0
but
parseInt("9") = 9
Why is the prefixed zero not just stripped out?
alert(parseInt("01")); = 1
.. rage quit
Because that is treated as octal format, by default. If you want to get 9, you must add number base you want, and thats 10, not 8 (for octal), so call:
parseInt("09", 10);
A.7. parseInt
parseInt is a function that converts a string into an integer. It stops when it sees a nondigit, so
parseInt("16")
and parseInt("16 tons")
produce the same result. It would be nice if the
function somehow informed us about the extra text, but it doesn't.
If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and
9 are not digits, so parseInt("08")
and parseInt("09")
produce 0 as their result. This error causes
problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that
parseInt("08", 10)
produces 8. I recommend that you always provide the radix parameter.
"JavaScript: The Good Parts by Douglas Crockford. Copyright 2008 Yahoo! Inc., 978-0-596-51774-8."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With