blob: 742abe51a646c26fd9219717e2b225f945fd98ad (
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
|
---
title: Number.isInteger()
slug: Web/JavaScript/Reference/Global_Objects/Number/isInteger
tags:
- JavaScript
- Method
- Number
- Reference
- 方法
translation_of: Web/JavaScript/Reference/Global_Objects/Number/isInteger
---
<div>{{JSRef}}</div>
<p><strong><code>Number.isInteger()</code></strong> 方法用来判断给定的参数是否为整数。</p>
<p>{{EmbedInteractiveExample("pages/js/number-isinteger.html")}}</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><code>Number.isInteger(v<var>alue</var>)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>value</code></dt>
<dd>要判断此参数是否为整数</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>判断给定值是否是整数的 {{jsxref("Boolean")}} 值。</p>
<h2 id="Description" name="Description">描述</h2>
<p>如果被检测的值是整数,则返回 <code>true</code>,否则返回 <code>false</code>。注意 {{jsxref("Global_Objects/NaN", "NaN")}} 和正负 {{jsxref("Global_Objects/Infinity", "Infinity")}} 不是整数。</p>
<h2 id="Example" name="Example">示例</h2>
<pre class="brush: js">Number.isInteger(0); // true
Number.isInteger(1); // true
Number.isInteger(-100000); // true
Number.isInteger(0.1); // false
Number.isInteger(Math.PI); // false
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger("10"); // false
Number.isInteger(true); // false
Number.isInteger(false); // false
Number.isInteger([1]); // false</pre>
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
<pre class="brush: js">Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范名称</th>
<th scope="col">规范状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-number.isinteger', 'Number.isInteger')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-number.isinteger', 'Number.isInteger')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.Number.isInteger")}}</p>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li>{{jsxref("Global_Objects/Number", "Number")}}</li>
</ul>
|