TypeScript: error when using parseInt() on a number

前端 未结 7 1105
心在旅途
心在旅途 2021-02-18 15:47

The JavaScript function parseInt can be used to force conversion of a given parameter to an integer, whether that parameter is a string, float number, number, etc.

7条回答
  •  独厮守ぢ
    2021-02-18 16:02

    The function parseInt indeed expects a string in its first argument. Please check the documentation. Usually you can omit the second, radix argument and then it will fall back to the default of 10. But the safest is to always add the numeric system base as second argument (usually 10).

    If you'd like to cast a general value to number, you can use the Number function, like this.

    var myNumber = Number(myGeneralValue);
    

提交回复
热议问题