{"id":304818,"date":"2024-05-20T19:31:24","date_gmt":"2024-05-20T11:31:24","guid":{"rendered":""},"modified":"2024-05-20T19:31:49","modified_gmt":"2024-05-20T11:31:49","slug":"%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%af%ad%e5%8f%a5%ef%bc%9a%e5%a6%82%e6%9e%9cx%e4%b8%8d%e7%ad%89%e4%ba%8e%e6%95%b4%e6%95%b0%ef%bc%8c%e5%88%99%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/304818.html","title":{"rendered":"\u5982\u4f55\u5b9e\u73b0\u8bed\u53e5\uff1a\u5982\u679cx\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u5219\u2026\u2026\uff1a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221100\/afab3018-c3a7-4dcd-952d-0971442ccff2.webp\" alt=\"\u5982\u4f55\u5b9e\u73b0\u8bed\u53e5\uff1a\u5982\u679cx\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u5219\u2026\u2026\uff1a\" \/><\/p>\n<p><p>\u5728\u7f16\u7a0b\u4e2d\uff0c<strong>\u5224\u65adx\u662f\u5426\u4e3a\u6574\u6570<\/strong>\u662f\u4e00\u9879\u57fa\u7840\u4f46\u6781\u5176\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u5b9e\u73b0\u8fd9\u4e00\u903b\u8f91\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u4f7f\u7528\u7c7b\u578b\u5224\u65ad\u51fd\u6570\u3001\u6bd4\u8f83x\u4e0e\u5176\u6574\u6570\u5f62\u5f0f\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u6d4b<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8de8\u8d8a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f46\u539f\u7406\u7c7b\u4f3c\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u7c7b\u578b\u5224\u65ad\u51fd\u6570<\/strong>\u663e\u5f97\u5c24\u4e3a\u76f4\u63a5\u548c\u9ad8\u6548\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f9d\u8d56\u4e8e\u8bed\u8a00\u63d0\u4f9b\u7684\u5185\u5efa\u51fd\u6570\u6216\u673a\u5236\uff0c\u6765\u5224\u5b9a\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u662f\u5426\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u6765\u5224\u65ad\u53d8\u91cf\u662f\u5426\u4e3a<code>int<\/code>\u7c7b\u578b\uff0c\u8fd9\u662f\u9a8c\u8bc1\u53d8\u91cf\u662f\u5426\u4e3a\u6574\u6570\u7684\u76f4\u63a5\u65b9\u5f0f\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6709\u6548\u5730\u5b9e\u73b0\u5bf9\u53d8\u91cf\u7c7b\u578b\u7684\u68c0\u6d4b\uff0c\u4ece\u800c\u6839\u636e\u68c0\u6d4b\u7ed3\u679c\u6267\u884c\u76f8\u5e94\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7c7b\u578b\u5224\u65ad\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u4e86\u5404\u79cd\u7c7b\u578b\u5224\u65ad\u7684\u51fd\u6570\u6216\u673a\u5236\u3002\u5728Python\u4e2d\uff0c<code>isinstance(variable, int)<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u5982\u679c\u53d8\u91cf<code>x<\/code>\u7684\u7c7b\u578b\u662f<code>int<\/code>\uff0c\u90a3\u4e48<code>isinstance(x, int)<\/code>\u5c06\u8fd4\u56deTrue\uff0c\u53cd\u4e4b\u5219\u8fd4\u56deFalse\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u662f\u5b9e\u73b0\u201c\u5982\u679cx\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u5219\u2026\u2026\u201d\u903b\u8f91\u7684\u9996\u9009\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Number.isInteger(x)<\/code>\u6765\u5224\u65ad<code>x<\/code>\u662f\u5426\u4e3a\u6574\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u68c0\u67e5<code>x<\/code>\u662f\u5426\u4e3a\u6574\u578b\u503c\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56deTrue\uff1b\u5982\u679c\u4e0d\u662f\u6574\u6570\uff08\u5305\u62ec\u6d6e\u70b9\u6570\u6216\u975e\u6570\u5b57\u503c\uff09\uff0c\u5219\u8fd4\u56deFalse\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6bd4\u8f83x\u4e0e\u5176\u6574\u6570\u5f62\u5f0f<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u5c06\u53d8\u91cfx\u4e0e\u5176\u8f6c\u6362\u6210\u6574\u6570\u540e\u7684\u5f62\u5f0f\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4e24\u8005\u76f8\u540c\uff0c\u8bf4\u660ex\u662f\u6574\u6570\uff1b\u5982\u679c\u4e0d\u540c\uff0c\u5219x\u4e0d\u662f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>int(x) == x<\/code>\u6765\u5b9e\u73b0\u8fd9\u4e00\u903b\u8f91\u3002\u8fd9\u91cc\uff0c<code>int(x)<\/code>\u5c1d\u8bd5\u5c06<code>x<\/code>\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u5982\u679c<code>x<\/code>\u5df2\u7ecf\u662f\u6574\u6570\uff0c\u90a3\u4e48<code>int(x)<\/code>\u5c31\u662f<code>x<\/code>\u672c\u8eab\uff0c\u56e0\u6b64\u6bd4\u8f83\u7ed3\u679c\u4e3aTrue\u3002\u5982\u679c<code>x<\/code>\u662f\u6d6e\u70b9\u6570\u6216\u5176\u4ed6\u975e\u6574\u6570\u5f62\u5f0f\uff0c\u8f6c\u6362\u7ed3\u679c\u4e0e\u539f\u503c\u4e0d\u540c\uff0c\u6bd4\u8f83\u7ed3\u679c\u4e3aFalse\u3002<\/p>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Math.floor(x) === x<\/code>\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\u3002<code>Math.floor(x)<\/code>\u51fd\u6570\u4f1a\u5411\u4e0b\u53d6\u6574\uff0c\u5982\u679c<code>x<\/code>\u5df2\u7ecf\u662f\u6574\u6570\uff0c\u90a3\u4e48\u7ed3\u679c\u4ecd\u662f<code>x<\/code>\u672c\u8eab\uff0c\u4e0e\u539f\u503c\u4e25\u683c\u76f8\u7b49\uff08\u5373\u7c7b\u578b\u548c\u503c\u90fd\u76f8\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5185\u5bb9\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u503c\u662f\u5426\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f<code>re.match(r&#039;^-?\\d+$&#039;, str(x))<\/code>\u6765\u5224\u65ad<code>x<\/code>\uff08\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff09\u662f\u5426\u4e3a\u6574\u6570\u3002\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u5339\u914d\u4efb\u4f55\u6574\u6570\u5f62\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u8d1f\u6574\u6570\u3002\u5982\u679c\u5339\u914d\u6210\u529f\uff0c<code>re.match()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5339\u914d\u5bf9\u8c61\uff1b\u5982\u679c\u5931\u8d25\uff0c\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<code>\/^-?\\d+$\/.test(String(x))<\/code>\u6765\u8fdb\u884c\u68c0\u6d4b\u3002\u8fd9\u91cc\uff0c<code>String(x)<\/code>\u5c06x\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e4b\u540e\u901a\u8fc7<code>.test()<\/code>\u65b9\u6cd5\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u7b26\u5408\u6574\u6570\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7279\u6b8a\u60c5\u5883\u4e0b\u7684\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7279\u5b9a\u7684\u7f16\u7a0b\u95ee\u9898\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u5230\u4e00\u4e9b\u7279\u6b8a\u7684\u60c5\u5883\u3002\u4f8b\u5982\uff0c\u5f53\u5904\u7406\u5927\u6570\u636e\u6216\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u503c\u65f6\uff08\u5982BigInt\u5728JavaScript\u4e2d\uff09\uff0c\u53ef\u80fd\u9700\u8981\u91c7\u7528\u7279\u5b9a\u7684\u65b9\u6cd5\u6765\u5224\u65ad\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u5904\u7406\u5927\u6574\u6570\u65f6\u9700\u8981\u7528\u5230<code>BigInt<\/code>\u7c7b\u578b\uff0c\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5224\u65ad\u7c7b\u578b\u6765\u786e\u8ba4\u53d8\u91cf\u662f\u5426\u4e3aBigInt\u7c7b\u578b\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55Number.isInteger\u65b9\u6cd5\u6765\u652f\u6301BigInt\uff0c\u4ece\u800c\u4f7f\u6211\u4eec\u7684\u6574\u6570\u68c0\u6d4b\u903b\u8f91\u66f4\u52a0\u901a\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5904\u7406\u5927\u6570\u6216\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u503c\uff08\u5982NumPy\u5e93\u4e2d\u7684\u6570\u503c\u7c7b\u578b\uff09\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5b9e\u9645\u4f7f\u7528\u573a\u666f\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u68c0\u6d4b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u7684\u5408\u7406\u9009\u62e9\u548c\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u5bf9\u53d8\u91cf\u201c\u5982\u679cx\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u5219\u2026\u2026\u201d\u7684\u903b\u8f91\u5224\u65ad\uff0c\u786e\u4fdd\u7a0b\u5e8f\u903b\u8f91\u7684\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5982\u4f55\u6839\u636ex\u7684\u503c\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff1f<\/strong><\/p>\n<p>A1: \u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u6839\u636e\u53d8\u91cf\u7684\u503c\u8fdb\u884c\u5224\u65ad\u3002\u5982\u679c\u8981\u5224\u65adx\u662f\u5426\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u7b49\u4e8e\u64cd\u4f5c\u7b26\uff08&quot;!=&quot;\uff09\u8fdb\u884c\u6bd4\u8f83\u3002\u6839\u636ex\u7684\u503c\u662f\u5426\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u4f60\u53ef\u4ee5\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p><strong>Q2: \u5728\u7f16\u7a0b\u4e2d\u5982\u4f55\u5b9e\u73b0\u9488\u5bf9x\u4e0d\u7b49\u4e8e\u6574\u6570\u7684\u5904\u7406\uff1f<\/strong><\/p>\n<p>A2: \u5982\u679c\u4f60\u60f3\u5728x\u4e0d\u7b49\u4e8e\u6574\u6570\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4e00\u4e9b\u7279\u5b9a\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2aif\u8bed\u53e5\u6765\u5224\u65adx\u7684\u503c\u662f\u5426\u4e0d\u7b49\u4e8e\u6574\u6570\u3002\u5982\u679c\u6761\u4ef6\u6210\u7acb\uff0c\u5373x\u4e0d\u7b49\u4e8e\u6574\u6570\uff0c\u4f60\u53ef\u4ee5\u5728if\u8bed\u53e5\u7684\u4ee3\u7801\u5757\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002\u8fd9\u6837\uff0c\u5f53x\u4e0d\u7b49\u4e8e\u6574\u6570\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u6267\u884cif\u8bed\u53e5\u4e2d\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<p><strong>Q3: \u5982\u4f55\u907f\u514dx\u7b49\u4e8e\u6574\u6570\u7684\u7279\u6b8a\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>A3: \u5982\u679c\u4f60\u5e0c\u671b\u5728\u7f16\u7a0b\u4e2d\u907f\u514dx\u7b49\u4e8e\u6574\u6570\u7684\u7279\u6b8a\u60c5\u51b5\u53d1\u751f\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u52a0\u5165\u4e00\u4e9b\u9632\u5fa1\u6027\u7684\u4ee3\u7801\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u68c0\u67e5\u6765\u786e\u4fddx\u7684\u7c7b\u578b\u662f\u5408\u9002\u7684\uff0c\u5e76\u5c06\u5176\u9650\u5236\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u5176\u6b21\uff0c\u4f60\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u9a8c\u8bc1\u903b\u8f91\uff0c\u4f8b\u5982\u4f7f\u7528\u65ad\u8a00\u8bed\u53e5\u6765\u786e\u4fddx\u7684\u503c\u4e0d\u662f\u6574\u6570\u3002\u6700\u540e\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u5728x\u7b49\u4e8e\u6574\u6570\u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u5e76\u5728\u5904\u7406\u65f6\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514dx\u7b49\u4e8e\u6574\u6570\u7684\u7279\u6b8a\u60c5\u51b5\u7684\u53d1\u751f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u7f16\u7a0b\u4e2d\uff0c\u5224\u65adx\u662f\u5426\u4e3a\u6574\u6570\u662f\u4e00\u9879\u57fa\u7840\u4f46\u6781\u5176\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u5b9e\u73b0\u8fd9\u4e00\u903b\u8f91\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7c7b\u578b\u5224\u65ad\u51fd\u6570\u3001\u6bd4\u8f83x\u4e0e\u5176 [&hellip;]","protected":false},"author":3,"featured_media":304848,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/304818"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=304818"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/304818\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/304848"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=304818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=304818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=304818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}