{"id":1010113,"date":"2024-12-27T11:18:02","date_gmt":"2024-12-27T03:18:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010113.html"},"modified":"2024-12-27T11:18:05","modified_gmt":"2024-12-27T03:18:05","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bd%acint","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1010113.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8f6cint"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084803\/8f7dc588-84f7-4469-9ac9-222f29fc2a9c.webp\" alt=\"python\u4e2d\u5982\u4f55\u8f6cint\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\uff08int\uff09\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>int()<\/code>\u6765\u5b9e\u73b0\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u8f6c\u6362\u3001\u4ee5\u53ca\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u3001\u6d6e\u70b9\u6570\u548c\u5176\u4ed6\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>int()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>int()<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u5c06\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u4e0b\u9762\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u662f\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\u6570\u5b57\uff0c<code>int()<\/code>\u53ef\u4ee5\u76f4\u63a5\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_str = &quot;123&quot;<\/p>\n<p>num_int = int(num_str)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\u88ab\u6210\u529f\u8f6c\u6362\u4e3a\u6574\u6570<code>123<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2. \u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u5f53\u6d6e\u70b9\u6570\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u5c0f\u6570\u90e8\u5206\u4f1a\u88ab\u622a\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_float = 123.456<\/p>\n<p>num_int = int(num_float)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc<code>123.456<\/code>\u88ab\u622a\u65ad\u4e3a\u6574\u6570<code>123<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u8f6c\u6362\u5e76\u4e0d\u662f\u56db\u820d\u4e94\u5165\uff0c\u53ea\u662f\u7b80\u5355\u7684\u622a\u65ad\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u8fdb\u5236\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>int()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u6765\u5c06\u4e00\u4e2a\u4e0d\u540c\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_bin = &quot;1010&quot;<\/p>\n<p>num_int = int(num_bin, 2)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<code>&quot;1010&quot;<\/code>\u88ab\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570<code>10<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u6765\u5b9e\u73b0\u6574\u6570\u8f6c\u6362\uff0c\u7279\u522b\u662f\u5f53\u6d89\u53ca\u8ba1\u7b97\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5f88\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u6d6e\u70b9\u6570\u7684\u622a\u65ad\u548c\u56db\u820d\u4e94\u5165<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>int()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u622a\u65ad\u6d6e\u70b9\u6570\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u56db\u820d\u4e94\u5165\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>int()<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_float = 123.456<\/p>\n<p>num_rounded = round(num_float)<\/p>\n<p>num_int = int(num_rounded)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>round()<\/code>\u51fd\u6570\u7528\u4e8e\u5148\u56db\u820d\u4e94\u5165\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u518d\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u5904\u7406\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5b57\u7b26\u4e32\u4e2d\u53ef\u80fd\u5305\u542b\u6570\u5b66\u8868\u8fbe\u5f0f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>eval()<\/code>\u51fd\u6570\u5148\u8ba1\u7b97\u51fa\u7ed3\u679c\uff0c\u518d\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">expression = &quot;10 + 20 * 3&quot;<\/p>\n<p>result = eval(expression)<\/p>\n<p>num_int = int(result)<\/p>\n<p>print(num_int)  # \u8f93\u51fa: 70<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc<code>eval()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff0c\u5c24\u5176\u5728\u9700\u8981\u751f\u6210\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u4e0d\u662f\u76f4\u63a5\u7684\u6574\u6570\u8f6c\u6362\uff0c\u4f46\u5728\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_int = 123<\/p>\n<p>formatted_str = f&quot;The number is {num_int}&quot;<\/p>\n<p>print(formatted_str)  # \u8f93\u51fa: The number is 123<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u683c\u5f0f\u5316\u8fdb\u884c\u8fdb\u5236\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5176\u4ed6\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num_int = 10<\/p>\n<p>formatted_bin = f&quot;{num_int:b}&quot;<\/p>\n<p>print(formatted_bin)  # \u8f93\u51fa: 1010<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5904\uff0c<code>f&quot;{num_int:b}&quot;<\/code>\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u6709\u4e00\u4e9b\u5e38\u89c1\u7684\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u7262\u8bb0\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u9519\u8bef\u6216\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>1. \u975e\u6570\u503c\u5b57\u7b26\u4e32\u8f6c\u6362\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u975e\u6570\u503c\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">non_num_str = &quot;abc&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(non_num_str)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u8f6c\u6362\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5904\u7406\u5927\u6570\u636e\u65f6\u7684\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u6570\u636e\u5904\u7406\u65f6\uff0c\u9891\u7e41\u7684\u7c7b\u578b\u8f6c\u6362\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u56e0\u6b64\u8981\u5c3d\u91cf\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>3. \u8fdb\u5236\u8f6c\u6362\u65f6\u7684\u6709\u6548\u6027<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5b57\u7b26\u4e32\u8868\u793a\u7684\u6570\u5b57\u7b26\u5408\u6307\u5b9a\u7684\u8fdb\u5236\u8303\u56f4\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">invalid_bin = &quot;102&quot;<\/p>\n<p>try:<\/p>\n<p>    num_int = int(invalid_bin, 2)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(f&quot;\u8fdb\u5236\u8f6c\u6362\u9519\u8bef: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u9760\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>int(&quot;123&quot;)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570<code>123<\/code>\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u5c06\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u8f6c\u6362\u4e4b\u524d\u786e\u4fdd\u5b57\u7b26\u4e32\u683c\u5f0f\u6b63\u786e\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u6d6e\u70b9\u6570\u53ef\u4ee5\u901a\u8fc7<code>int()<\/code>\u51fd\u6570\u76f4\u63a5\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u8f6c\u6362\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>int(12.7)<\/code>\u5c06\u8fd4\u56de<code>12<\/code>\u3002\u5982\u679c\u5e0c\u671b\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\uff0c\u4e4b\u540e\u518d\u5c06\u7ed3\u679c\u4f20\u5165<code>int()<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>numpy.int32()<\/code>\u6216<code>numpy.int64()<\/code>\u7b49\u65b9\u6cd5\u6765\u8fdb\u884c\u66f4\u7279\u5b9a\u7684\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u6027\u80fd\u548c\u5185\u5b58\u7ba1\u7406\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u5229\u7528<code>.astype(int)<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u6846\u4e2d\u7684\u5217\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\uff08int\uff09\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570int()\u6765\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528int( [&hellip;]","protected":false},"author":3,"featured_media":1010120,"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\/1010113"}],"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=1010113"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010113\/revisions"}],"predecessor-version":[{"id":1010121,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010113\/revisions\/1010121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010120"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}