{"id":1136124,"date":"2025-01-08T21:32:27","date_gmt":"2025-01-08T13:32:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136124.html"},"modified":"2025-01-08T21:32:30","modified_gmt":"2025-01-08T13:32:30","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%b0%8f%e6%95%b0%e5%9b%9b%e8%88%8d%e4%ba%94%e5%85%a5%e5%8f%96%e6%95%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136124.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u5c0f\u6570\u56db\u820d\u4e94\u5165\u53d6\u6574"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104615\/38ba3e06-109c-498e-975a-93234b539432.webp\" alt=\"python\u5982\u4f55\u5bf9\u5c0f\u6570\u56db\u820d\u4e94\u5165\u53d6\u6574\" \/><\/p>\n<p><p> <strong>Python \u5bf9\u5c0f\u6570\u56db\u820d\u4e94\u5165\u53d6\u6574\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<code>round()<\/code>\u51fd\u6570\u3001<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>ceil()<\/code>\u548c<code>floor()<\/code>\u51fd\u6570\u3001<code>decimal<\/code>\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong><code>round()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001<code>round()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u5bf9\u6570\u5b57\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u5176\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">round(number, ndigits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>number<\/code>\uff1a\u9700\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\u3002<\/li>\n<li><code>ndigits<\/code>\uff1a\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u53d6\u6574\u3002<\/li>\n<\/ul>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(round(3.14159, 2))  # \u8f93\u51fa\uff1a3.14<\/p>\n<p>print(round(3.5))         # \u8f93\u51fa\uff1a4<\/p>\n<p>print(round(3.4))         # \u8f93\u51fa\uff1a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1\u3001\u56db\u820d\u4e94\u5165\u89c4\u5219<\/h3>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u9075\u5faa\u5e38\u89c4\u7684\u56db\u820d\u4e94\u5165\u89c4\u5219\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5c0f\u6570\u70b9\u540e\u7b2c\u4e00\u4f4d\u5927\u4e8e\u7b49\u4e8e5\u65f6\uff0c\u5411\u4e0a\u53d6\u6574\uff1b\u5c0f\u4e8e5\u65f6\uff0c\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6b63\u597d\u5728\u4e2d\u95f4\u7684\u6570\u5b57\uff08\u4f8b\u59820.5\uff09\uff0c<code>round()<\/code>\u51fd\u6570\u4f1a\u5411\u8ddd\u79bb\u5076\u6570\u66f4\u8fd1\u7684\u65b9\u5411\u820d\u5165\u3002\u8fd9\u79cd\u884c\u4e3a\u79f0\u4e3a\u201c\u94f6\u884c\u5bb6\u820d\u5165\u6cd5\u201d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(round(0.5))  # \u8f93\u51fa\uff1a0<\/p>\n<p>print(round(1.5))  # \u8f93\u51fa\uff1a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>ceil()<\/code>\u548c<code>floor()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u820d\u5165\u51fd\u6570\uff0c\u5982<code>ceil()<\/code>\u548c<code>floor()<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u7528\u4e8e\u5411\u4e0a\u53d6\u6574\u548c\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.ceil(3.1))  # \u8f93\u51fa\uff1a4<\/p>\n<p>print(math.floor(3.9)) # \u8f93\u51fa\uff1a3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1\u3001<code>math.ceil()<\/code><\/h3>\n<\/p>\n<p><p><code>math.ceil()<\/code>\u51fd\u6570\u8fd4\u56de\u5927\u4e8e\u6216\u7b49\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5c0f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001<code>math.floor()<\/code><\/h3>\n<\/p>\n<p><p><code>math.floor()<\/code>\u51fd\u6570\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5927\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001<code>decimal<\/code>\u6a21\u5757<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\u3002<code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Decimal<\/code>\u7c7b\u578b\u548c\u56db\u820d\u4e94\u5165\u7684\u65b9\u5f0f\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, ROUND_HALF_UP<\/p>\n<p>num = Decimal(&#39;3.14159&#39;)<\/p>\n<p>rounded_num = num.quantize(Decimal(&#39;1.00&#39;), rounding=ROUND_HALF_UP)<\/p>\n<p>print(rounded_num)  # \u8f93\u51fa\uff1a3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1\u3001\u9ad8\u7cbe\u5ea6\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u8d22\u52a1\u8ba1\u7b97\u6216\u5176\u4ed6\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u573a\u666f\uff0c\u907f\u514d\u4e86\u6d6e\u70b9\u6570\u7684\u8bef\u5dee\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u7075\u6d3b\u7684\u820d\u5165\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u820d\u5165\u6a21\u5f0f\uff0c\u5982<code>ROUND_HALF_UP<\/code>\u3001<code>ROUND_DOWN<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u3002\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\u5305\u62ec<code>f-string<\/code>\u3001<code>format()<\/code>\u65b9\u6cd5\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>formatted_num = &quot;{:.2f}&quot;.format(num)<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa\uff1a3.14<\/p>\n<p>formatted_num_f_string = f&quot;{num:.2f}&quot;<\/p>\n<p>print(formatted_num_f_string)  # \u8f93\u51fa\uff1a3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1\u3001<code>str.format()<\/code><\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>str.format()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h3>2\u3001<code>f-string<\/code><\/h3>\n<\/p>\n<p><p><code>f-string<\/code>\u662fPython 3.6\u5f15\u5165\u7684\u65b0\u7279\u6027\uff0c\u4f7f\u7528\u8d77\u6765\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u591a\u79cd\u5bf9\u5c0f\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u53d6\u6574\u7684\u65b9\u5f0f\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u4ee3\u7801\u66f4\u52a0\u9ad8\u6548\u548c\u7cbe\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5c0f\u6570\u7684\u56db\u820d\u4e94\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u56db\u820d\u4e94\u5165\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5e0c\u671b\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>round(3.14159, 2)<\/code>\u4f1a\u8fd4\u56de<code>3.14<\/code>\u3002\u5982\u679c\u5e0c\u671b\u5bf9\u6574\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u53ef\u4ee5\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7701\u7565\uff0c\u76f4\u63a5\u4f7f\u7528<code>round(3.5)<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>4<\/code>\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5c0f\u6570\u8fdb\u884c\u53d6\u6574\uff1f<\/strong><br \/>\u9664\u4e86\u56db\u820d\u4e94\u5165\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u5c0f\u6570\u8fdb\u884c\u53d6\u6574\u3002\u4f7f\u7528<code>math.floor()<\/code>\u53ef\u4ee5\u5411\u4e0b\u53d6\u6574\uff0c\u8fd4\u56de\u4e0d\u5927\u4e8e\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5927\u6574\u6570\u3002\u76f8\u53cd\uff0c<code>math.ceil()<\/code>\u5219\u5411\u4e0a\u53d6\u6574\uff0c\u8fd4\u56de\u4e0d\u5c0f\u4e8e\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5c0f\u6574\u6570\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5b83\u4f1a\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u5c0f\u6570\u53d6\u6574\uff1f<\/strong><br \/>\u5904\u7406\u8d1f\u6570\u65f6\uff0c\u56db\u820d\u4e94\u5165\u548c\u53d6\u6574\u7684\u7ed3\u679c\u53ef\u80fd\u4e0e\u6b63\u6570\u6709\u6240\u4e0d\u540c\u3002\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u5bf9\u8d1f\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u65f6\uff0c\u4f8b\u5982<code>round(-2.5)<\/code>\uff0c\u7ed3\u679c\u662f<code>-2<\/code>\u3002\u4f7f\u7528<code>math.floor()<\/code>\u548c<code>math.ceil()<\/code>\u5bf9\u8d1f\u6570\u7684\u53d6\u6574\u7ed3\u679c\u4e5f\u4e0e\u6b63\u6570\u76f8\u53cd\uff0c<code>math.floor(-2.5)<\/code>\u8fd4\u56de\u7684\u662f<code>-3<\/code>\uff0c\u800c<code>math.ceil(-2.5)<\/code>\u8fd4\u56de\u7684\u662f<code>-2<\/code>\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u8d1f\u6570\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u53d6\u6574\u7684\u65b9\u5411\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5bf9\u5c0f\u6570\u56db\u820d\u4e94\u5165\u53d6\u6574\u7684\u65b9\u6cd5\u5305\u62ec\uff1around()\u51fd\u6570\u3001math\u6a21\u5757\u4e2d\u7684ceil()\u548cfloor()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1136130,"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\/1136124"}],"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=1136124"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136124\/revisions"}],"predecessor-version":[{"id":1136131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136124\/revisions\/1136131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136130"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}