Revision 447255 of Number.isInteger()

  • Revision slug: JavaScript/Reference/Global_Objects/Number/isInteger
  • Revision title: Number.isInteger
  • Revision id: 447255
  • Created:
  • Creator: zhangyaochun1987
  • Is current revision? No
  • Comment share for Chinese

Revision Content

{{harmony}} {{fx_minversion_header("15")}}

概述

判定指定的参数是否是整数

 

Method of Number
Implemented in JavaScript 1.8.5+
ECMAScript Edition ECMAScript 6th Edition

语法

Number.isInteger(testValue)

参数

testValue
判定此参数是否是整数

描述

判定指定的参数是否是整数。如果是,返回true,否则返回false。

示例

alert([
  Number.isInteger(0.1),
  Number.isInteger(1),
  Number.isInteger(Math.PI)
]);

Polyfill

The following algorithm matches the one specified in ECMA harmony:

/*\
|*|
|*|  :: Number.isInteger() polyfill ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
|*|
\*/

if (!Number.isInteger) {
  Number.isInteger = function isInteger (nVal) {
    return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
  };
}

浏览器兼容

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 25.0 {{todo("? ")}} {{CompatGeckoDesktop("15")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatGeckoDesktop("15")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

 

Revision Source

<div>
  {{harmony}} {{fx_minversion_header("15")}}</div>
<h2 id="Summary" name="Summary">概述</h2>
<p>判定指定的参数是否是整数</p>
<p>&nbsp;</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of<code> <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number" title="JavaScript/Reference/Global_Objects/Number">Number</a></code></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code>Number.isInteger(<var>testValue</var>)</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
  <dt>
    <code><var>testValue</var></code></dt>
  <dd>
    判定此参数是否是整数</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>判定指定的参数是否是整数。如果是,返回true,否则返回false。</p>
<h2 id="Example" name="Example">示例</h2>
<pre class="brush:js">
alert([
  Number.isInteger(0.1),
  Number.isInteger(1),
  Number.isInteger(Math.PI)
]);</pre>
<h2 id="Polyfill" name="Polyfill">Polyfill</h2>
<p>The following algorithm matches <a href="http://wiki.ecmascript.org/doku.php?id=harmony:number.isinteger" title="http://wiki.ecmascript.org/doku.php?id=harmony:number.isinteger">the one specified in ECMA harmony</a>:</p>
<pre class="brush:js;">
/*\
|*|
|*|  :: Number.isInteger() polyfill ::
|*|
|*|  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
|*|
\*/

if (!Number.isInteger) {
&nbsp; Number.isInteger = function isInteger (nVal) {
&nbsp;&nbsp;&nbsp; return typeof nVal === "number" &amp;&amp; isFinite(nVal) &amp;&amp; nVal &gt; -9007199254740992 &amp;&amp; nVal &lt; 9007199254740992 &amp;&amp; Math.floor(nVal) === nVal;
&nbsp; };
}</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>25.0 {{todo("?&nbsp;")}}</td>
        <td>{{CompatGeckoDesktop("15")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatGeckoDesktop("15")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatNo}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision