开发者

javascript problem with parseint [duplicate]

开发者 https://www.devze.com 2023-03-12 23:17 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Workarounds for JavaScript parseInt octal bug
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Workarounds for JavaScript parseInt octal bug

I am trying to parse an integer number.

a = parseInt("0005")  <- gives 5
a = parseInt("0008")  <- gives 0

Can someone explain what's happe开发者_开发问答ning? It doesn't make any sense to me.


When parseInt has a leading 0 and a radix parameter isn't specified, it assumes you want to convert the number to octal. Instead you should always specify a radix parameter like so:

a = parseInt("0008", 10) // => 8


Numbers starting with 0 are parsed as octal by parseInt, unless you specify a radix to use.

You can force parseInt to parse as decimal by doing

a = parseInt("0008", 10)
0

精彩评论

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