{"id":1016016,"date":"2024-12-27T12:12:55","date_gmt":"2024-12-27T04:12:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016016.html"},"modified":"2024-12-27T12:12:58","modified_gmt":"2024-12-27T04:12:58","slug":"python-%e5%a6%82%e4%bd%95%e5%90%91%e4%b8%8b%e5%8f%96%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016016.html","title":{"rendered":"python \u5982\u4f55\u5411\u4e0b\u53d6\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095713\/3af8f4b6-018f-4c6a-8156-997e8b1d5694.webp\" alt=\"python \u5982\u4f55\u5411\u4e0b\u53d6\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.floor()<\/code>\u51fd\u6570\u3001<code>int()<\/code>\u51fd\u6570\u3001<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u3001<code>math.trunc()<\/code>\u51fd\u6570\u5b9e\u73b0\u5411\u4e0b\u53d6\u6574\u3002<\/strong>\u5176\u4e2d\uff0c<code>math.floor()<\/code>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u5411\u4e0b\u53d6\u6574\uff0c\u5c06\u6d6e\u70b9\u6570\u5411\u4e0b\u53d6\u6574\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c<code>int()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fbe\u5230\u7c7b\u4f3c\u6548\u679c\uff0c\u4f46\u4f1a\u76f4\u63a5\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u9002\u7528\u4e8e\u6b63\u6570\u3002\u9664\u6b64\u4e4b\u5916\uff0c<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u53ef\u4ee5\u7528\u4e8e\u6574\u6570\u9664\u6cd5\u5e76\u5f97\u5230\u5546\u7684\u6574\u6570\u90e8\u5206\uff0c\u800c<code>math.trunc()<\/code>\u51fd\u6570\u7528\u4e8e\u622a\u65ad\u5c0f\u6570\u90e8\u5206\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>MATH.FLOOR()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>math.floor()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u6570\u5b66\u51fd\u6570\u4e4b\u4e00\uff0c\u4e13\u95e8\u7528\u4e8e\u5411\u4e0b\u53d6\u6574\u3002\u5b83\u4f1a\u5c06\u4e00\u4e2a\u6d6e\u70b9\u6570\u820d\u5165\u5230\u4e0d\u5927\u4e8e\u8be5\u6570\u7684\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002\u4f7f\u7528\u65f6\u9700\u8981\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.floor(5.8)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>math.floor()<\/code>\u662f\u975e\u5e38\u7cbe\u786e\u7684\u5411\u4e0b\u53d6\u6574\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u603b\u662f\u5c06\u503c\u51cf\u5c11\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u8f93\u5165\u7684\u6b63\u8d1f\u503c\u4e0d\u540c\u800c\u6539\u53d8\u884c\u4e3a\u3002\u65e0\u8bba\u8f93\u5165\u662f\u6b63\u6570\u8fd8\u662f\u8d1f\u6570\uff0c<code>math.floor()<\/code>\u90fd\u80fd\u6b63\u786e\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e-2.3\uff0c<code>math.floor()<\/code>\u7684\u7ed3\u679c\u662f-3\uff0c\u800c\u4e0d\u662f-2\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<code>INT()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>int()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u51fd\u6570\u3002\u5b83\u4f1a\u76f4\u63a5\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4e8e\u5411\u4e0b\u53d6\u6574\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b83\u5bf9\u8d1f\u6570\u7684\u5904\u7406\u65b9\u5f0f\u4e0e<code>math.floor()<\/code>\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = int(5.8)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6b63\u6570\u6765\u8bf4\uff0c<code>int()<\/code>\u51fd\u6570\u7684\u6548\u679c\u4e0e<code>math.floor()<\/code>\u76f8\u540c\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u8d1f\u6570\u6765\u8bf4\uff0c<code>int()<\/code>\u662f\u5411\u96f6\u53d6\u6574\uff0c\u800c\u4e0d\u662f\u5411\u4e0b\u53d6\u6574\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e-2.3\uff0c<code>int()<\/code>\u7684\u7ed3\u679c\u662f-2\uff0c\u800c<code>math.floor()<\/code>\u7684\u7ed3\u679c\u662f-3\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001<code>\/\/<\/code>\u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>\/\/<\/code>\u662f\u6574\u6570\u9664\u6cd5\u8fd0\u7b97\u7b26\u3002\u5b83\u8fd4\u56de\u9664\u6cd5\u8fd0\u7b97\u7684\u6574\u6570\u90e8\u5206\uff0c\u81ea\u52a8\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 5.8 \/\/ 1<\/p>\n<p>print(result)  # \u8f93\u51fa: 5.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u5728\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u5546\u7684\u6574\u6570\u90e8\u5206\u800c\u4e0d\u9700\u8981\u989d\u5916\u7684\u51fd\u6570\u8c03\u7528\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u8fd4\u56de\u7684\u662f\u6d6e\u70b9\u6570\u7c7b\u578b\uff0c\u9664\u975e\u88ab\u9664\u6570\u548c\u9664\u6570\u90fd\u662f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001<code>MATH.TRUNC()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>math.trunc()<\/code>\u51fd\u6570\u7528\u4e8e\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u8fd4\u56de\u6570\u5b57\u7684\u6574\u6570\u90e8\u5206\u3002\u8fd9\u4e0e<code>int()<\/code>\u7684\u884c\u4e3a\u76f8\u4f3c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u622a\u65ad\u5c0f\u6570\u800c\u4e0d\u662f\u4e25\u683c\u7684\u5411\u4e0b\u53d6\u6574\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>result = math.trunc(5.8)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>math.trunc()<\/code>\u5728\u5904\u7406\u6b63\u6570\u65f6\u4e0e<code>math.floor()<\/code>\u6548\u679c\u76f8\u540c\uff0c\u4f46\u5728\u5904\u7406\u8d1f\u6570\u65f6\uff0c\u5b83\u4e0e<code>int()<\/code>\u76f8\u540c\uff0c\u662f\u5411\u96f6\u53d6\u6574\u800c\u4e0d\u662f\u5411\u4e0b\u53d6\u6574\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4e0d\u540c\u65b9\u6cd5\u7684\u6bd4\u8f83\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7cbe\u786e\u5411\u4e0b\u53d6\u6574<\/strong>\uff1a\u5982\u679c\u9700\u8981\u7cbe\u786e\u7684\u5411\u4e0b\u53d6\u6574\uff0c\u65e0\u8bba\u8f93\u5165\u662f\u6b63\u6570\u8fd8\u662f\u8d1f\u6570\uff0c<code>math.floor()<\/code>\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u603b\u662f\u5c06\u6570\u503c\u964d\u4f4e\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b80\u5355\u622a\u65ad<\/strong>\uff1a\u5982\u679c\u53ea\u9700\u8981\u7b80\u5355\u7684\u622a\u65ad\u64cd\u4f5c\uff0c<code>int()<\/code>\u548c<code>math.trunc()<\/code>\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u5c24\u5176\u5728\u5904\u7406\u6b63\u6570\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6574\u6570\u9664\u6cd5<\/strong>\uff1a\u5f53\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u4e0d\u4ec5\u80fd\u5411\u4e0b\u53d6\u6574\uff0c\u8fd8\u80fd\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\uff0c\u662f\u5904\u7406\u6574\u6570\u8fd0\u7b97\u65f6\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u7684\u6bd4\u8f83\u4e0e\u5e94\u7528\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5411\u4e0b\u53d6\u6574\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\u3002\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5dee\u5f02\u53ca\u5176\u9002\u7528\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5411\u4e0b\u53d6\u6574\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5411\u4e0b\u53d6\u6574\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>floor()<\/code>\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u5b57\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e\u8be5\u6570\u5b57\u7684\u6700\u5927\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>math.floor(3.7)<\/code>\u4f1a\u8fd4\u56de3\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6574\u9664\u8fd0\u7b97\u7b26<code>\/\/<\/code>\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u5982<code>5.9 \/\/ 1<\/code>\u4e5f\u4f1a\u8fd4\u56de5\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u8fdb\u884c\u5411\u4e0b\u53d6\u6574\u662f\u5426\u53ef\u884c\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>int()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5411\u4e0b\u53d6\u6574\uff0c\u4f46\u8981\u6ce8\u610f\uff0c\u5b83\u4f1a\u76f4\u63a5\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u8fdb\u884c\u6570\u5b66\u4e0a\u7684\u5411\u4e0b\u53d6\u6574\u3002\u4f8b\u5982\uff0c<code>int(3.9)<\/code>\u5c06\u8fd4\u56de3\uff0c\u800c<code>int(-3.1)<\/code>\u5c06\u8fd4\u56de-3\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u4e8e\u8d1f\u6570\uff0c<code>int()<\/code>\u5e76\u4e0d\u603b\u662f\u8fd4\u56de\u6700\u63a5\u8fd1\u7684\u4e0b\u9650\u503c\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u5904\u7406\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5411\u4e0b\u53d6\u6574\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u6570\u636e\u5904\u7406\u65f6\uff0c\u9009\u62e9\u5411\u4e0b\u53d6\u6574\u7684\u65b9\u6cd5\u5e94\u8003\u8651\u6570\u636e\u7684\u6027\u8d28\u4ee5\u53ca\u5e0c\u671b\u5b9e\u73b0\u7684\u76ee\u6807\u3002\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u6b63\u6570\u4e14\u53ea\u9700\u7b80\u5355\u5730\u53bb\u6389\u5c0f\u6570\u90e8\u5206\uff0c\u4f7f\u7528<code>int()<\/code>\u6216<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u662f\u5feb\u901f\u4e14\u6709\u6548\u7684\u9009\u62e9\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8d1f\u6570\u6216\u8005\u5bf9\u7cbe\u786e\u5ea6\u6709\u66f4\u9ad8\u8981\u6c42\uff0c\u4f7f\u7528<code>math.floor()<\/code>\u4f1a\u66f4\u4e3a\u5408\u9002\uff0c\u56e0\u4e3a\u5b83\u80fd\u786e\u4fdd\u7ed3\u679c\u59cb\u7ec8\u662f\u6700\u63a5\u8fd1\u7684\u4e0b\u9650\u6574\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528math.floor()\u51fd\u6570\u3001int()\u51fd\u6570\u3001\/\/\u8fd0\u7b97\u7b26\u3001math.trunc()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1016027,"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\/1016016"}],"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=1016016"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016016\/revisions"}],"predecessor-version":[{"id":1016028,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016016\/revisions\/1016028"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016027"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}