{"id":1087639,"date":"2025-01-08T13:37:00","date_gmt":"2025-01-08T05:37:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087639.html"},"modified":"2025-01-08T13:37:02","modified_gmt":"2025-01-08T05:37:02","slug":"python%e8%ae%a1%e7%ae%97%e5%a6%82%e4%bd%95%e4%bf%9d%e7%95%99%e4%b8%80%e4%bd%8d%e5%b0%8f%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087639.html","title":{"rendered":"python\u8ba1\u7b97\u5982\u4f55\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200456\/88c63d9d-3211-49bb-8de1-413598b5c333.webp\" alt=\"python\u8ba1\u7b97\u5982\u4f55\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8ba1\u7b97\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570round()\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001Decimal\u6a21\u5757\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002<\/strong> \u5176\u4e2d\uff0cround()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u4e14\u6ee1\u8db3\u5927\u591a\u6570\u65e5\u5e38\u9700\u6c42\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528round()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570round()\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u820d\u5165\u7684\u6570\u5b57\u548c\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>rounded_num = round(num, 1)<\/p>\n<p>print(rounded_num)  # \u8f93\u51fa: 3.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>round()\u51fd\u6570\u7684\u4f18\u70b9\u5728\u4e8e\u4f7f\u7528\u65b9\u4fbf\u3001\u65e0\u9700\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\uff0c\u4e14\u80fd\u6ee1\u8db3\u5927\u591a\u6570\u57fa\u7840\u9700\u6c42\u3002\u7136\u800c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cround()\u51fd\u6570\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u94f6\u884c\u5bb6\u820d\u5165\u6cd5\uff08\u53c8\u79f0\u56db\u820d\u516d\u5165\u4e94\u5355\u53cc\uff09\u7684\u5e94\u7528\uff0c\u8fd9\u5728\u67d0\u4e9b\u8d22\u52a1\u8ba1\u7b97\u4e2d\u53ef\u80fd\u4f1a\u5f15\u53d1\u7cbe\u5ea6\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u503c\uff08f-strings\uff09\u548cstr.format()\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528f-strings\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>formatted_num = f&quot;{num:.1f}&quot;<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa: 3.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\u7684\u6548\u679c\u4e5f\u53ef\u4ee5\u901a\u8fc7str.format()\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>formatted_num = &quot;{:.1f}&quot;.format(num)<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa: 3.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u4f18\u70b9\u5728\u4e8e\u7075\u6d3b\u6027\u9ad8\u3001\u6613\u8bfb\u6027\u597d\uff0c\u9002\u5408\u5728\u751f\u6210\u62a5\u544a\u6216\u7528\u6237\u754c\u9762\u5c55\u793a\u65f6\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Decimal\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684decimal\u6a21\u5757\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86Decimal\u6570\u636e\u7c7b\u578b\uff0c\u80fd\u907f\u514d\u4e8c\u8fdb\u5236\u6d6e\u70b9\u6570\u8bef\u5dee\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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;0.1&#39;), rounding=ROUND_HALF_UP)<\/p>\n<p>print(rounded_num)  # \u8f93\u51fa: 3.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>Decimal\u6a21\u5757\u9002\u7528\u4e8e\u91d1\u878d\u8ba1\u7b97\u6216\u79d1\u5b66\u8ba1\u7b97\u7b49\u5bf9\u7cbe\u5ea6\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u80fd\u6709\u6548\u907f\u514d\u6d6e\u70b9\u6570\u8bef\u5dee\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u624b\u52a8\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">num = 3.14159<\/p>\n<p>rounded_num = int(num * 10) \/ 10<\/p>\n<p>print(rounded_num)  # \u8f93\u51fa: 3.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\uff0c\u4f46\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\uff0c\u4e0d\u63a8\u8350\u5728\u590d\u6742\u5e94\u7528\u4e2d\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u591a\u79cdPython\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\uff0c\u5305\u62ecround()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001decimal\u6a21\u5757\u548c\u6570\u5b66\u8fd0\u7b97\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002\u603b\u4f53\u6765\u8bf4\uff0c<strong>round()\u51fd\u6570\u548c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u800cDecimal\u6a21\u5757\u5219\u9002\u7528\u4e8e\u5bf9\u7cbe\u5ea6\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u6765\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u3002\u4f8b\u5982\uff0c<code>round(3.14159, 1)<\/code>\u5c06\u8fd4\u56de3.1\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4f60\u60f3\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u6765\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\uff0c\u6bd4\u5982\u4f7f\u7528<code>f&quot;{value:.1f}&quot;<\/code>\u7684\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u8f93\u51fa\u4e3a\u4e00\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u8981\u683c\u5f0f\u5316\u8f93\u51fa\u4e3a\u4e00\u4f4d\u5c0f\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216f-string\u3002\u5728\u4f7f\u7528<code>format()<\/code>\u65f6\uff0c\u53ef\u4ee5\u5199\u6210<code>&quot;{:.1f}&quot;.format(value)<\/code>\uff0c\u800c\u5728f-string\u4e2d\u5219\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>f&quot;{value:.1f}&quot;<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u4f1a\u5c06\u6570\u503c\u683c\u5f0f\u5316\u4e3a\u4e00\u4f4d\u5c0f\u6570\uff0c\u5e76\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8f93\u51fa\uff0c\u975e\u5e38\u9002\u5408\u5728\u6253\u5370\u6216\u663e\u793a\u6570\u636e\u65f6\u4f7f\u7528\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5728Python\u8ba1\u7b97\u4e2d\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u8ba1\u7b97\u4e2d\uff0c\u5982\u679c\u9700\u8981\u786e\u4fdd\u7ed3\u679c\u59cb\u7ec8\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u5728\u8be5\u51fd\u6570\u4e2d\u4f7f\u7528<code>round()<\/code>\u6765\u5904\u7406\u8f93\u5165\u7684\u6570\u503c\uff0c\u5e76\u8fd4\u56de\u683c\u5f0f\u5316\u540e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<code>def keep_one_decimal(num): return round(num, 1)<\/code>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u9700\u8981\u65f6\u8c03\u7528\uff0c\u786e\u4fdd\u6240\u6709\u8ba1\u7b97\u7ed3\u679c\u90fd\u7b26\u5408\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8ba1\u7b97\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570round()\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001Decimal\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1087650,"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\/1087639"}],"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=1087639"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087639\/revisions"}],"predecessor-version":[{"id":1087653,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087639\/revisions\/1087653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087650"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}