aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/parsefloat/index.html
blob: 6469340da8259f48af79a9176be36b1f9c0999f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
---
title: parseFloat
slug: Web/JavaScript/Reference/Global_Objects/parseFloat
tags:
  - parseFloat()
translation_of: Web/JavaScript/Reference/Global_Objects/parseFloat
---
<div>
<div>
<div>{{jsSidebar("Objects")}}</div>
</div>
</div>

<p><strong><code>parseFloat()</code> </strong>函数解析一个参数(必要时先转换为字符串)并返回一个浮点数。</p>

<div>{{EmbedInteractiveExample("pages/js/globalprops-parsefloat.html")}}</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">parseFloat(<em>string)</em></pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>string</code></dt>
 <dd>需要被解析成为浮点数的值。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<dl>
 <dd>给定值被解析成浮点数。如果给定值不能被转换成数值,则会返回 {{jsxref("NaN")}}</dd>
</dl>

<h2 id="描述">描述</h2>

<p><code>parseFloat</code>是个全局函数,不属于任何对象。</p>

<ul>
 <li>如果 <code>parseFloat</code> 在解析过程中遇到了正号(<code>+</code>)、负号(<code>-</code> U+002D HYPHEN-MINUS)、数字(<code>0</code>-<code>9</code>)、小数点(<code>.</code>)、或者科学记数法中的指数(e 或 E)以外的字符,则它会忽略该字符以及之后的所有字符,返回当前已经解析到的浮点数。</li>
 <li>第二个小数点的出现也会使解析停止(在这之前的字符都会被解析)。</li>
 <li>参数首位和末位的空白符会被忽略。</li>
 <li>如果参数字符串的第一个字符不能被解析成为数字,<code></code> <code>parseFloat</code> 返回 <code>NaN</code></li>
 <li><code>parseFloat</code> 也可以解析并返回 {{jsxref("Infinity")}}</li>
 <li><code>parseFloat</code>解析 {{jsxref("BigInt")}}{{jsxref("Number", "Numbers")}}, 丢失精度。因为末位 <code>n</code> 字符被丢弃。</li>
</ul>

<p>考虑使用 {{jsxref("Number", "Number(<em>value</em>)")}} 进行更严谨的解析,只要参数带有无效字符就会被转换为 {{jsxref("NaN")}}</p>

<p><code>parseFloat</code> 也可以转换一个已经定义了 <code>toString</code> 或者 <code>valueOf</code> 方法的对象,它返回的值和在调用该方法的结果上调用 <code>parseFloat</code> 值相同。</p>

<h2 id="例子">例子</h2>

<h3 id="例子_parseFloat返回正常数字">例子: <code>parseFloat</code>返回正常数字</h3>

<p>下面的例子都返回<strong>3.14</strong></p>

<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="number token">3.14</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'3.14'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'  3.14  '</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'314e-2'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'0.0314E+2'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'3.14some non-digit characters'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="punctuation token">{</span> <span class="function function-variable token">toString</span><span class="punctuation token">:</span> <span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> <span class="string token">"3.14"</span> <span class="punctuation token">}</span> <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<h3 id="parseFloat返回NaN"><code>parseFloat</code>返回NaN</h3>

<p>下面的例子将返回<code>NaN</code></p>

<pre class="brush: js">parseFloat("FF2");
</pre>

<h3 id="parseFloat_和_BigInt">parseFloat 和 BigInt</h3>

<p>以下例子均返回 <code>900719925474099300</code>,当整数太大以至于不能被转换时将失去精度。</p>

<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="number token">900719925474099267n</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="function token">parseFloat</span><span class="punctuation token">(</span><span class="string token">'900719925474099267n'</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.1.2.3', 'parseFloat')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-parsefloat-string', 'parseFloat')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-parsefloat-string', 'parseFloat')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("javascript.builtins.parseFloat")}}</p>

<h2 id="See_Also" name="See_Also">相关链接</h2>

<ul>
 <li>{{jsxref("Global_Objects/parseInt", "parseInt()")}}</li>
 <li>{{jsxref("Number.parseFloat()")}}</li>
 <li>{{jsxref("Number.parseInt()")}}</li>
 <li>{{jsxref("Global_Objects/isNaN", "isNaN()")}}</li>
</ul>