﻿{"id":916,"date":"2014-06-01T15:21:09","date_gmt":"2014-06-01T07:21:09","guid":{"rendered":"http:\/\/notes.zerobox.org\/?p=916"},"modified":"2014-06-01T15:21:09","modified_gmt":"2014-06-01T07:21:09","slug":"%e5%9c%a8javascript%e4%b8%ad%e5%88%a4%e6%96%ad%e6%95%b4%e5%9e%8b%e7%9a%84n%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/zerobox.org\/notes\/916.html","title":{"rendered":"\u5728JavaScript\u4e2d\u5224\u65ad\u6574\u578b\u7684N\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p>\u6574\u6570\u7c7b\u578b\uff08Integer\uff09\u5728JavaScript\u7ecf\u5e38\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u5947\u602a\u7684\u95ee\u9898\u3002\u5728ECMAScript\u7684\u89c4\u8303\u4e2d\uff0c\u4ed6\u4eec\u53ea\u5b58\u5728\u4e8e\u6982\u5ff5\u4e2d\uff1a<\/p>\n<blockquote><p>\u6240\u6709\u7684\u6570\u5b57\u90fd\u662f\u6d6e\u70b9\u6570\uff0c\u5e76\u4e14\u6574\u6570\u53ea\u662f\u6ca1\u6709\u4e00\u7ec4\u6ca1\u6709\u5c0f\u6570\u7684\u6570\u5b57\u3002<\/p><\/blockquote>\n<p>\u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\uff0c\u6211\u4f1a\u89e3\u91ca\u5982\u4f55\u53bb\u68c0\u67e5\u67d0\u4e2a\u503c\u662f\u5426\u4e3a\u6574\u578b\u3002<\/p>\n<h3>ECMAScript 5<\/h3>\n<p>\u5728ES5\u4e2d\u6709\u5f88\u591a\u65b9\u6cd5\u4f60\u53ef\u4ee5\u4f7f\u7528\u3002\u6709\u65f6\u4faf\uff0c\u4f60\u53ef\u80fd\u60f3\u7528\u81ea\u5df1\u7684\u65b9\u6cd5\uff1a\u4e00\u4e2aisInteger(x)\u7684\u51fd\u6570\uff0c\u5982\u679c\u662f\u6574\u578b\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse.<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u4e00\u4e9b\u4f8b\u5b50\u3002<\/p>\n<p><b>\u901a\u8fc7\u4f59\u6570\u68c0\u67e5<\/b><\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f59\u6570\u8fd0\u7b97\uff08%\uff09\uff0c\u5c06\u4e00\u4e2a\u6570\u5b57\u63091\u6c42\u4f59\uff0c\u770b\u770b\u4f59\u6570\u662f\u4e0d\u662f0\u3002<\/p>\n<pre>function isInteger(x) {\r\n\u00a0\u00a0\u00a0 return x % 1 === 0;\r\n}<\/pre>\n<p>\u6211\u559c\u6b22\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u975e\u5e38\u7b80\u5355\uff0c\u800c\u4e14\u6709\u6548\u3002<\/p>\n<pre>&gt; isInteger(17)\r\ntrue\r\n&gt; isInteger(17.13)\r\nfalse<\/pre>\n<p>\u5728\u64cd\u4f5c\u4f59\u6570\u8fd0\u7b97\u65f6\u4f60\u5f97\u5c0f\u5fc3\u4e00\u70b9\uff0c\u56e0\u4e3a\u7ed3\u679c\u53d6\u51b3\u4e8e\u7b2c\u4e00\u4e2a\u6570\u7684\u7b26\u53f7\uff0c\u5982\u679c\u662f\u6b63\u7684\uff0c\u7ed3\u679c\u5c31\u662f\u6b63\u7684\uff1b\u5426\u5219\u5c31\u662f\u8d1f\u7684\u3002<\/p>\n<pre>&gt; 3.5 % 1\r\n0.5\r\n&gt; -3.5 % 1\r\n-0.5<\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u68c0\u67e50\uff0c\u8fd9\u5176\u5b9e\u4e0d\u662f\u4e00\u4e2a\u95ee\u9898\u3002\u4f46\u95ee\u9898\u662f\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u5bf9\u975e\u6570\u5b57\u4e5f\u4f1a\u8fd4\u56detrue\uff0c\u56e0\u4e3a % \u4f1a\u5c06\u5b83\u8f6c\u6362\u6210\u6570\u5b57\uff1a<\/p>\n<pre>&gt; isInteger('')\r\ntrue\r\n&gt; isInteger('33')\r\ntrue\r\n&gt; isInteger(false)\r\ntrue\r\n&gt; isInteger(true)\r\ntrue<\/pre>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5f88\u7b80\u5355\u7684\u7c7b\u578b\u68c0\u67e5\u6765\u89e3\u51b3\uff1a<\/p>\n<pre>function isInteger(x) {\r\n\u00a0\u00a0\u00a0 return (typeof x === 'number') &amp;&amp; (x % 1 === 0);\r\n}<\/pre>\n<p><b>\u901a\u8fc7Math.round()<\/b><\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u6570\u5b57\u53d6\u6574\u4ee5\u540e\u8fd8\u8ddf\u4e4b\u524d\u7684\u503c\u4e00\u6837\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u6574\u6570\u3002\u5728JavaScript\u4e2d\u53ef\u4ee5\u901a\u8fc7Math.round()\u6765\u68c0\u67e5\uff1a<\/p>\n<pre>function isInteger(x) {\r\n\u00a0\u00a0\u00a0 return Math.round(x) === x;\r\n}<\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u4e0d\u9519<\/p>\n<pre>&gt; isInteger(17)\r\ntrue\r\n&gt; isInteger(17.13)\r\nfalse<\/pre>\n<p>\u5b83\u4e5f\u53ef\u4ee5\u5224\u65ad\u975e\u6570\u5b57\uff0c\u56e0\u4e3aMath.round()\u603b\u662f\u8fd4\u56de\u6570\u5b57\uff0c\u5e76\u4e14===\u53ea\u6709\u5f53\u7c7b\u578b\u4e00\u6837\u65f6\u624d\u8fd4\u56detrue.<\/p>\n<pre>&gt; isInteger('')\r\nfalse<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u8ba9\u4ee3\u7801\u66f4\u6e05\u6670\u4e00\u70b9\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u7c7b\u578b\u68c0\u67e5\uff08\u5c31\u662f\u6211\u4eec\u5728\u4e4b\u524d\u7248\u672c\u505a\u7684\uff09\u3002\u53e6\u5916\uff0cMath.floor()\u548cMath.ceil()\u53ef\u4ee5\u50cfMath.round()\u4e00\u6837\u5de5\u4f5c\u3002<\/p>\n<p><b>\u901a\u8fc7\u4f4d\u64cd\u4f5c\u68c0\u67e5<\/b><\/p>\n<p>\u4f4d\u64cd\u4f5c\u7b26\u63d0\u4f9b\u53e6\u5916\u4e00\u79cd\u201c\u53d6\u6574\u201d\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>function isInteger(x) {\r\n\u00a0\u00a0\u00a0 return (x | 0) === x;\r\n}<\/pre>\n<p>\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\uff08\u8ddf\u5176\u5b83\u4f4d\u8fd0\u7b97\u4e00\u6837\uff09\u6709\u4e00\u4e2a\u7f3a\u9677\uff1a\u5b83\u65e0\u6cd5\u5904\u7406\u8d85\u8fc732\u4f4d\u7684\u6570\u5b57\u3002<\/p>\n<pre>&gt; isInteger(Math.pow(2, 32))\r\nfalse<\/pre>\n<p><b>\u901a\u8fc7parseInt()\u68c0\u67e5<\/b><\/p>\n<p>parseInt()\u4e5f\u63d0\u4f9b\u4e86\u8ddfMath.round()\u7c7b\u4f3c\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u6574\u578b\u7684\u65b9\u6cd5\u3002<br \/>\n\u8ba9\u6211\u4eec\u770b\u770b\u8fd9\u4e2a\u65b9\u6cd5\u4e3a\u4ec0\u4e48\u4e0d\u9519\u3002<\/p>\n<pre>function isInteger(x) {\r\n\u00a0\u00a0\u00a0 return parseInt(x, 10) === x;\r\n}<\/pre>\n<p>\u50cfMath.round()\u89e3\u51b3\u65b9\u6848\u4e00\u6837\uff0c\u5b83\u4e5f\u53ef\u4ee5\u5904\u7406\u975e\u6570\u5b57\u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5b83\u4e5f\u4e0d\u80fd\u6b63\u786e\u5730\u5904\u7406\u6240\u6709\u7684\u6574\u578b\u6570\u5b57\uff1a<\/p>\n<pre>&gt; isInteger(1000000000000000000000)\r\nfalse<\/pre>\n<p>\u4e3a\u4ec0\u4e48\uff1fparseInt()\u5728\u89e3\u6790\u6574\u6570\u4e4b\u524d\u5f3a\u8feb\u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u89e3\u6790\u6210\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u6574\u578b\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u9009\u62e9\u3002<\/p>\n<pre>&gt; parseInt(1000000000000000000000, 10)\r\n1\r\n&gt; String(1000000000000000000000)\r\n'1e+21'<\/pre>\n<p>\u5c31\u50cf\u4e0a\u9762\u90a3\u6837\uff0cparseInt()\u5728\u89e3\u6790&#8217;1e+21&#8217;\u65f6\u57281\u5904\u505c\u6b62\u5904\u7406\u4e86\uff0c\u6240\u4ee5\u5b83\u624d\u4f1a\u8fd4\u56de1.<\/p>\n<h3>ECMAScript 6<\/h3>\n<p>\u5bf9\u4e8eMath.round()\u6254\u8865\u5145\uff0cES6\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u4e2a\u5c06\u6570\u5b57\u8f6c\u6362\u6210\u6574\u578b\u7684\u65b9\u6cd5\uff1aMath.trunc()\u3002\u8be5\u51fd\u6570\u4f1a\u79fb\u9664\u6570\u5b57\u7684\u5c0f\u6570\u90e8\u5206\u3002<\/p>\n<pre>&gt; Math.trunc(4.1)\r\n4\r\n&gt; Math.trunc(4.9)\r\n4\r\n&gt; Math.trunc(-4.1)\r\n-4\r\n&gt; Math.trunc(-4.9)\r\n-4<\/pre>\n<p>\u6b64\u5916\uff0cECMAScript6\u4e0d\u9700\u8981\u53bb\u5904\u7406\u68c0\u67e5\u6574\u6570\u90a3\u4e9b\u7410\u788e\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u5b83\u5e26\u6709\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570 Number.isInteger\uff08\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d:http:\/\/ourjs.com\/detail\/538a72e34929582e62000008<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6574\u6570\u7c7b\u578b\uff08Integer\uff09\u5728JavaScript\u7ecf\u5e38\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u5947\u602a\u7684\u95ee\u9898\u3002\u5728EC &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/zerobox.org\/notes\/916.html\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[117],"class_list":["post-916","post","type-post","status-publish","format-standard","hentry","tag-javascript"],"views":452,"_links":{"self":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/comments?post=916"}],"version-history":[{"count":0,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/posts\/916\/revisions"}],"wp:attachment":[{"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/media?parent=916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/categories?post=916"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zerobox.org\/notes\/wp-json\/wp\/v2\/tags?post=916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}