parseInt() parseInt() 函式能將輸入的字串轉成整數。 嘗試一下 </div></section><section aria-labelledby="語法"><h2 id="語法"><a href="#語法">語法</a></h2><div class="section-content"><pre class="brush: plain notranslate">parseInt(string, radix); <dt id="string"><code>string</code></dt> <p>待轉成數字的字串。若 <code>string</code> 參數類型不是字串的話,會先將其轉成字串(相當於先執行 <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-tostring" class="external" target="_blank"><code>ToString</code></a> 再執行 <code>parseInt</code>)空白值會被忽略。</p> <dt id="radix"><code>radix</code></dt> <p>從 2 到 36,能代表該進位系統的數字。例如說指定 <code>10</code> 就等於指定十進位。<strong>一定要定義這個參數</strong>以避免他人的困惑、也好預估函式的行為。如果沒有指定 radix 的話,給出的結果會按照實做不同而異,請注意,通常預設值<strong>不是</strong> 10 進位。</p> </dl></div></section><section aria-labelledby="回傳值"><h3 id="回傳值"><a href="#回傳值">回傳值</a></h3><div class="section-content"><p>藉由給定字串作轉換後的數字。若第一個字符無法轉換為數字,則回傳 <a href="/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/NaN"><code>NaN</code></a>。</p></div></section><section aria-labelledby="說明"><h2 id="說明"><a href="#說明">說明</a></h2><div class="section-content"><p><code>parseInt</code> 函式會把第一個參數變成字串、解析它、再回傳整數或是 <code>NaN</code>。如果不是 <code>NaN</code>,回傳值會把第一個參數,參照指定的 <em>radix</em> 後,以十進位表示。例如,<em>radix</em> 指定為 10 的話,它會以十進位為單位轉換、8 是八進位、16 是十六進位,依此類推。For radices above <code>10</code>, the letters of the alphabet indicate numerals greater than <code>9</code>. For example, for hexadecimal numbers (base 16), <code>A</code> through <code>F</code> are used.</p> <p>如果說 <code>parseInt</code> 碰上了無法被 radix 指定的進位制所轉換的字元,它會忽略該字元、以及其後所有字元,並只回傳至該位置為止的解析數值結果。<code>parseInt</code> 將數字擷取、轉換成整數數值。 可以接受字串首尾出現空白。</p> <p>Because some numbers include the <code>e</code> character in their string representation (e.g. <strong><code>6.022e23</code></strong>), using <code>parseInt</code> to truncate numeric values will produce unexpected results when used on very large or very small numbers. <code>parseInt</code> should not be used as a substitute for <a href="/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Math/floor"><code>Math.floor()</code></a>.</p> <p>如果 <em>radix</em> 是 <code>undefined</code> 或 0(或留空)的話,JavaScript 會:</p> <li>如果 <code>string</code> 由 "0x" 或 "0X" 開始,<em>radix</em> 會變成代表十六進位的 16,並解析字串的餘數。</li> <li>如果 <code>string</code> 由 0 開始,則 <em>radix</em> 會變成代表八進位的 8 或十進位的 10,但到底會變成 8 還是 10 則取決於各實做。ECMAScript 規定用代表十進位的 10,但也不是所有瀏覽器都支持。因此,<strong>使用 <code>parseInt</code> 時一定要指定 radix</strong>。</li> <li>如果 <code>string</code> 由其他字串開始,radix 就會是十進位的 10。</li> <p>如果第一個字串無法被解析為任何數字,<code>parseInt</code> 會回傳 <code>NaN</code>。</p> <p>For arithmetic purposes, the <code>NaN</code> value is not a number in any radix. You can call the <a href="/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/isNaN"><code>isNaN</code></a> function to determine if the result of <code>parseInt</code> is <code>NaN</code>. If <code>NaN</code> is passed on to arithmetic operations, the operation results will also be <code>NaN</code>.</p> <p>若想將數字轉成特定的進位制,可使用 <code>intValue.toString(radix)</code>。</p></div></section><section aria-labelledby="範例"><h2 id="範例"><a href="#範例">範例</a></h2><div class="section-content"/></section><section aria-labelledby="使用_parseint"><h3 id="使用_parseint"><a href="#使用_parseint">使用 <code>parseInt</code></a></h3><div class="section-content"><p>以下的範例,回傳的值均為 <strong><code>15</code></strong>:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">" 0xF"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">" F"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"17"</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">021</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"015"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// parseInt(015, 10); will return 15</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">15.99</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"15,123"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"FXX123"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"1111"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"15*3"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"15e2"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"15px"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"12"</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <p>以下均回傳 <strong><code>NaN</code></strong>:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"Hello"</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 根本不是數字</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"546"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 在二進位無效</span> <p>以下的範例,回傳的值均為 <strong><code>-15</code></strong>:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-F"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-0F"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-0XF"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">15.1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">" -17"</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">" -15"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-1111"</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-15e1"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"-12"</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <p>下例會回傳 <strong><code>4</code></strong>:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">4.7</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">4.7</span> <span class="token operator">*</span> <span class="token number">1e22</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Very large number becomes 4</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">0.00000000000434</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Very small number becomes 4</span> <p>下例會回傳 <strong><code>224</code></strong>:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"0e0"</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre></span></p></div></div></section><section aria-labelledby="無_radix_情況下的八進制"><h2 id="無_radix_情況下的八進制"><a href="#無_radix_情況下的八進制">無 radix 情況下的八進制</a></h2><div class="section-content"><p>雖說已在 ECMAScript 3 提議並於 ECMAScript 5 禁用,但部分 javascript 編譯器仍會在特殊情況下,將 str 視作八進位數字(當數字以 <code>0</code> 開頭時)。以下為可能發生這種問題的情況:(<strong>永遠要宣告 radix 以避開這不可靠的行為</strong>)</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"0e0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"08"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0, '8' is not an octal digit.</span> </code></pre></span></p></div></div></section><section aria-labelledby="ecmascript_5_移除八進位轉譯(octal_interpretation)"><h3 id="ecmascript_5_移除八進位轉譯(octal_interpretation)"><a href="#ecmascript_5_移除八進位轉譯(octal_interpretation)">ECMAScript 5 移除八進位轉譯(octal interpretation)</a></h3><div class="section-content"><p>The ECMAScript 5 specification of the function <code>parseInt</code> no longer allows implementations to treat Strings beginning with a <code>0</code> character as octal values. ECMAScript 5 states:</p> <p>The <code>parseInt</code> function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or <code>0</code>, it is assumed to be <code>10</code> except when the number begins with the character pairs <code>0x</code> or <code>0X</code>, in which case a radix of 16 is assumed.</p> <p>This differs from ECMAScript 3, which discouraged but allowed octal interpretation.</p> <p>Many implementations have not adopted this behavior as of 2013, and because older browsers must be supported, <strong>always specify a radix</strong>.</p></div></section><section aria-labelledby="嚴謹的解析_function"><h2 id="嚴謹的解析_function"><a href="#嚴謹的解析_function">嚴謹的解析 function</a></h2><div class="section-content"><p>有的時候,使用更嚴謹的 code 能夠更精確地轉換整數值。 Regular expressions 可以幫你:</p> <div class="code-example"><p class="example-header"><span class="language-name">js<pre class="brush: js notranslate"><code><span class="token function-variable function">filterInt</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><span class="token anchor function">^</span><span class="token group punctuation">(</span><span class="token escape">\-</span><span class="token alternation keyword">|</span><span class="token special-escape escape">\+</span><span class="token group punctuation">)</span><span class="token quantifier number">?</span><span class="token group punctuation">(</span><span class="token char-class"><span class="token char-class-punctuation punctuation">[</span><span class="token range">0<span class="token range-punctuation operator">-</span>9</span><span class="token char-class-punctuation punctuation">]</span></span><span class="token quantifier number">+</span><span class="token alternation keyword">|</span>Infinity<span class="token group punctuation">)</span><span class="token anchor function">$</span></span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">Number</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">NaN</span><span class="token punctuation">;</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'421'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 421</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'-421'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// -421</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'+421'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 421</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'Infinity'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Infinity</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'421e+0'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// NaN</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'421hop'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// NaN</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'hop1.61803398875'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// NaN</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">filterInt</span><span class="token punctuation">(</span><span class="token string">'1.61803398875'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// NaN</span> </code></pre></span></p></div></div></section><h2 id="規範"><a href="#規範">規範</a></h2><table class="standard-table"><thead><tr><th scope="col">Specification</th></tr></thead><tbody><tr><td><a href="https://tc39.es/ecma262/multipage/global-object.html#sec-parseint-string-radix">ECMAScript Language Specification<!-- --> <br/><small># <!-- -->sec-parseint-string-radix</small></a></td></tr></tbody></table><h2 id="瀏覽器相容性"><a href="#瀏覽器相容性">瀏覽器相容性</a></h2><p>BCD tables only load in the browser</p><noscript> <!-- -->with JavaScript enabled. Enable JavaScript to view data.</noscript><section aria-labelledby="延伸閱讀"><h2 id="延伸閱讀"><a href="#延伸閱讀">延伸閱讀</a></h2><div class="section-content"><ul> <li><a class="only-in-en-us" title="Currently only available in English (US)" href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat"><code>parseFloat()</code> <small>(en-US)</small></a></li>