{"id":1098209,"date":"2025-01-08T15:19:06","date_gmt":"2025-01-08T07:19:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098209.html"},"modified":"2025-01-08T15:19:09","modified_gmt":"2025-01-08T07:19:09","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e5%b0%8f%e6%95%b0%e7%82%b9%e5%90%8e%e9%9d%a2%e7%9a%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098209.html","title":{"rendered":"python\u5982\u4f55\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212631\/bd5bb6c8-0144-4723-8d60-add18f472636.webp\" alt=\"python\u5982\u4f55\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\" \/><\/p>\n<p><p> <strong>Python\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528int()\u51fd\u6570\u3001\u4f7f\u7528math\u6a21\u5757\u7684trunc()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5176\u4e2d\uff0c\u4f7f\u7528int()\u51fd\u6570\u662f\u6700\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528int()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>int()\u51fd\u6570\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4ece\u800c\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\u3002\u8be5\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>result = int(num)<\/p>\n<p>print(result)  # \u8f93\u51fa 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u76f4\u63a5\u5c06\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u90e8\u5206\u622a\u65ad\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u5982\u679c\u9700\u8981\u56db\u820d\u4e94\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528round()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528math\u6a21\u5757\u7684trunc()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>math\u6a21\u5757\u63d0\u4f9b\u4e86trunc()\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u90e8\u5206\u622a\u65ad\u3002\u8be5\u65b9\u6cd5\u4e0eint()\u51fd\u6570\u7c7b\u4f3c\uff0c\u4f46\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>num = 3.14159<\/p>\n<p>result = math.trunc(num)<\/p>\n<p>print(result)  # \u8f93\u51fa 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0eint()\u51fd\u6570\u4e00\u6837\uff0ctrunc()\u51fd\u6570\u4e5f\u4e0d\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u53ea\u662f\u7b80\u5355\u5730\u622a\u65ad\u5c0f\u6570\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fdb\u884c\u683c\u5f0f\u5316\u64cd\u4f5c\u6765\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u8f93\u51fa\u683c\u5f0f\u8fdb\u884c\u66f4\u7075\u6d3b\u63a7\u5236\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>result = &quot;{:.0f}&quot;.format(num)<\/p>\n<p>print(result)  # \u8f93\u51fa 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u81ea\u52a8\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u5982\u679c\u4e0d\u9700\u8981\u56db\u820d\u4e94\u5165\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528split()\u65b9\u6cd5\u5206\u5272\u5c0f\u6570\u70b9\u524d\u540e\u7684\u90e8\u5206\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>result = str(num).split(&#39;.&#39;)[0]<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;3&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u53ea\u662f\u7b80\u5355\u5730\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u64cd\u4f5c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u6548\u679c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001numpy\u5e93\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u66f4\u4e3a\u9002\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>num = 3.14159<\/p>\n<p>result = re.sub(r&#39;\\..*&#39;, &#39;&#39;, str(num))<\/p>\n<p>print(result)  # \u8f93\u51fa &#39;3&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\uff0c<strong>\u5176\u4e2d\u4f7f\u7528int()\u51fd\u6570\u662f\u6700\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u800cmath\u6a21\u5757\u7684trunc()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u7b49\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5982\u679c\u53ea\u662f\u5355\u7eaf\u5730\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\uff0c\u5efa\u8bae\u4f7f\u7528int()\u51fd\u6570\u6216math.trunc()\u51fd\u6570\uff1b\u5982\u679c\u9700\u8981\u5bf9\u8f93\u51fa\u683c\u5f0f\u8fdb\u884c\u66f4\u7075\u6d3b\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u9009\u62e9\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u800c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528numpy\u5e93\u7b49\u66f4\u9ad8\u6548\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u800c\u53bb\u6389\u5c0f\u6570\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u90e8\u5206\u3002\u4f8b\u5982\uff0c<code>int(3.14)<\/code>\u5c06\u8fd4\u56de<code>3<\/code>\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u76f4\u63a5\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u53bb\u6389Python\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u90e8\u5206\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>math.floor()<\/code>\u6216<code>math.ceil()<\/code>\u51fd\u6570\u3002<code>math.floor()<\/code>\u4f1a\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e\u7ed9\u5b9a\u6570\u7684\u6700\u5927\u6574\u6570\uff0c\u800c<code>math.ceil()<\/code>\u5219\u4f1a\u8fd4\u56de\u5927\u4e8e\u6216\u7b49\u4e8e\u7ed9\u5b9a\u6570\u7684\u6700\u5c0f\u6574\u6570\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6709\u6548\u53bb\u6389\u5c0f\u6570\u90e8\u5206\uff0c\u4f46\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u8fd4\u56de\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u6d6e\u70b9\u6570\u4ee5\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216f-string\u53ef\u4ee5\u6709\u6548\u53bb\u6389\u5c0f\u6570\u90e8\u5206\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<code>print(f&quot;{value:.0f}&quot;)<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570<code>value<\/code>\u683c\u5f0f\u5316\u4e3a\u6ca1\u6709\u5c0f\u6570\u7684\u5f62\u5f0f\u8fdb\u884c\u8f93\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53bb\u6389\u5c0f\u6570\u70b9\u540e\u9762\u7684\u51e0\u79cd\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528int()\u51fd\u6570\u3001\u4f7f\u7528math\u6a21\u5757\u7684trunc()\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c [&hellip;]","protected":false},"author":3,"featured_media":1098224,"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\/1098209"}],"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=1098209"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098209\/revisions"}],"predecessor-version":[{"id":1098227,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098209\/revisions\/1098227"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}