开发者

parseInt in Javascript doesn't give back expected value [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Javascript parseInt gives very unexpected results

Can someone explain why javascript interprets parseInt(08) and parseInt(09) as zero but does everything right on 01 to 07?

parseFloat will give me the correct values for those values, I just couldn't find an answer, why javascript will only do this to the 8s and 9s.

For your information: the leading zeros are from times (hours:minutes).

Please have开发者_如何学运维 a look at the following link for a better example: http://jsfiddle.net/NPtzA/3/ There you can see this unexpected behaviour.


parseInt returns 0 because the function is trying to determine the right base the numerical system it's looking at.

Whether it's a good thing or not, JavaScript numbers with a leading 0 are all considered as Octal which means there is no 08 or 09.

Hope that helps.

Edit: Check out my edit of your fiddle which adds 10 parameter to parseInt to change the numerical system it uses http://jsfiddle.net/jbrooksuk/NPtzA/5/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜