js string 转数字
可以使用 parseInt() 或 parseFloat() 方法将字符串转换为数字。
parseInt() 方法将字符串转换为整数,例如:
let str = "123";
let num = parseInt(str);
console.log(num); // 123
parseFloat() 方法将字符串转换为浮点数,例如:
let str = "3.14";
let num = parseFloat(str);
console.log(num); // 3.14
需要注意的是,如果字符串不能被转换为有效的数字,则返回 NaN(Not a Number)。例如:
let str = "abc";
let num = parseInt(str);
console.log(num); // NaN
原文地址: https://www.cveoy.top/t/topic/crlO 著作权归作者所有。请勿转载和采集!