JS的数据类型转换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
var num1 ="3.14"
var ret1 = parseFloat(num1) // #将字符串转换为数字
console.log(ret1,typeof ret1) // 3.14 'number'
var num2 ="100件"
var ret2 = parseInt(num2)
console.log(ret2,typeof ret2) // 100 'number' #将字符串转换为数字
var num3 ="第100件"
var ret3 = parseInt(num3)
console.log(ret3,typeof ret3) // NaN 'number' #将字符串转换为数字
var num4 = 3.14
var ret4 = num4.toString() // 3.14 strings #将数字转换为字符串
console.log(ret4,typeof ret4)
</script>
</head>
<body>
<h1>welcome to JS!</h1>
</body>
</html>
![](https://img2022.cnblogs.com/blog/1413093/202206/1413093-20220601202050779-1469256404.png)