aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/number/parseint/index.html
blob: e1b466489f019cbc5be49a4d51f835aed80da988 (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
---
title: Number.parseInt()
slug: Web/JavaScript/Reference/Global_Objects/Number/parseInt
tags:
  - ECMAScript 2015
  - JavaScript
  - Method
  - Number
translation_of: Web/JavaScript/Reference/Global_Objects/Number/parseInt
---
<div>{{JSRef}}</div>

<h2 id="Summary">概述</h2>

<p><strong><code>Number.parseInt()</code></strong> 方法依据指定基数 [ 参数 <strong>radix </strong>的值],把字符串 [ 参数 <strong>string</strong> 的值] 解析成整数。</p>

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

<pre class="syntaxbox"><code>Number.parseInt(<var>string</var>[, <var>radix</var>])</code>
</pre>

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

<p>{{page("en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt", "Parameters")}} </p>

<p>参数string:要解析的值。 如果此参数不是字符串,则使用ToString抽象操作将其转换为字符串。忽略此参数中的前导空格。<br>
 参数radix:一个介于2到36之间的整数,代表字符串的基数(数学数字系统中的基)。小心-这并不是默认为10。</p>

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

<p>{{page("en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt", "Return value")}}</p>

<p>从给定字符串中解析的整数。如果基数小于11,且第一个非空白字符不能转换为数字,则返回NaN。</p>

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

<p>这个方法和全局的 {{jsxref("parseInt", "parseInt()")}} 函数具有一样的函数功能:</p>

<pre><code>Number.parseInt === parseInt; // true</code></pre>

<p>ECMAScript 2015添加了这部分 (其目的是对全局变量进行模块化). 请另见 {{jsxref("parseInt", "parseInt()")}} 获取更多详情和示例.</p>

<h2 id="Polyfill">Polyfill</h2>

<pre><code>if (Number.parseInt === undefined) {
    Number.parseInt = window.parseInt;
}</code></pre>

<h2 id="说明">说明</h2>

<table>
 <tbody>
  <tr>
   <th scope="col">说明</th>
   <th scope="col">状态</th>
   <th scope="col">评论</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-number.parseint', 'Number.parseInt')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>初始定义.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-number.parseint', 'Number.parseInt')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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

<p>The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

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

<h2 id="See_also">相关链接</h2>

<ul>
 <li><code>{{jsxref("Global_Objects/Number", "Number()")}}</code></li>
 <li>全局的 {{jsxref("Global_Objects/parseInt", "parseInt()")}} 函数</li>
</ul>