Javascript, why treated as octal

前端 未结 6 526
我在风中等你
我在风中等你 2020-12-06 20:58

I\'m passing as parameter an id to a javascript function, because it comes from UI, it\'s left zero padded. but it seems to have (maybe) \"strange\" behaviour?



        
6条回答
  •  执念已碎
    2020-12-06 21:54

    If you receive your parameters as string objects, it should work to use

     parseInt(string, 10)
    

    to interpret strings as decimal, even if they are beginning with 0.

    In your test, you pass the parseInt method a number, not a string, maybe that's why it doesn't return the expected result.

    Try

     parseInt('0000022115', 10)
    

    instead of

    parseInt(0000022115, 10)
    

    that does return 221115 for me.

提交回复
热议问题