企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**转换成数值类型:** 1. Number() 2. parseInt() **两种方法转换结果对比:** | 示例 | Number() | parseInt | | --- | --- | --- | | '123' | 123 | 123 | | 'abc' | NaN | NaN | | false | 0 | NaN | | 'abc789' | NaN | NaN | | '123abc456' | NaN | 123 | 示例: ``` var s1 = '123'; var s2 = true; var s3 = 'abc456'; var s4 = '123def789'; var s5 = 'def789aaa852'; console.log('-------Number()--------'); // Number() 方法 console.log(Number(s1)); //输出:123 console.log(Number(s2)); //输出:1 console.log(Number(s3)); //输出:NaN console.log(Number(s4)); //输出:NaN console.log('-------parseInt()--------'); //parseInt() 方法 console.log(parseInt(s1)); //输出:123 console.log(parseInt(s2)); //输出:NaN console.log(parseInt(s3)); //输出:NaN console.log(parseInt(s4)); //输出:123 ``` ![](https://img.kancloud.cn/36/da/36da340c078f3b4b1d9372c21dc68bd6_650x264.png)