parseInt()不是函数

时间:2016-11-27 20:08:20

标签: react-native

当我尝试parseInt()时,它说:

  

_this2.state.grade.parseInt不是函数

代码:

this.state = {grade: "1"}

let grade = this.state.grade.parseInt()

此功能在React Native中不起作用吗?如果是这样,我怎么能把它转换成int?

2 个答案:

答案 0 :(得分:9)

尝试:

Number.parseInt(this.state.grade, 10)
// the 2nd argument is the base mathematical
// system to use for parsing and should
// always be passed to avoid confusion and
// ensure predictable behaviour

答案 1 :(得分:0)

parseInt 是全局函数,因为ECMAScript 2015的一部分可能不可用。

或者使用:

Number.parseInt(值,基数)

基本很重要,因为它会产生不同的值:

十进制

Number.parseInt( '011', 8)
-> 9

十进制

Number.parseInt( '011', 10)
->11

十六进制十进制

Number.parseInt( '011', 16)
    ->17