CodeLab
Show Output
 
xxxxxxxxxx
17
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="utf-8">
5
    <title>JavaScript Parse Floating Point Numbers from Strings</title>
6
</head>
7
<body>
8
    <script>
9
    document.write(parseFloat("3.14") + "<br>");  // 3.14
10
    document.write(parseFloat("50px") + "<br>");  // 50
11
    document.write(parseFloat("1.6em") + "<br>");  // 1.6
12
    document.write(parseFloat("124.5 lbs") + "<br>");  // 124.5
13
    document.write(parseFloat("weight 124.5 lbs") + "<br>");  // NaN
14
    document.write(parseFloat("6.5 acres"));  // 6.5
15
    </script>
16
</body>
17
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。