How do I work around JavaScript's parseInt octal behavior?

后端 未结 10 1633
Happy的楠姐
Happy的楠姐 2020-11-21 07:36

Try executing the following in JavaScript:

parseInt(\'01\'); //equals 1
parseInt(\'02\'); //equals 2
parseInt(\'03\'); //equals 3
parseInt(\'04\'); //equals          


        
10条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 07:44

    function parseDecimal(s) { return parseInt(s, 10); }
    

    edit: making your own function, to do what you really want, is just an option if you don't like adding the ",10" all the time to the parseInt() call. It has the disadvantage of being a nonstandard function: more convenient for you if you use it a lot, but perhaps more confusing for others.

提交回复
热议问题