{"id":1080847,"date":"2025-01-08T12:34:01","date_gmt":"2025-01-08T04:34:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080847.html"},"modified":"2025-01-08T12:34:03","modified_gmt":"2025-01-08T04:34:03","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e6%95%b0%e5%ad%97%e6%98%be%e7%a4%ba%e4%b8%a4%e4%bd%8d-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080847.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u6570\u5b57\u663e\u793a\u4e24\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183200\/99efd560-6467-4f23-97c1-73568a9bedab.webp\" alt=\"python\u5982\u4f55\u8ba9\u6570\u5b57\u663e\u793a\u4e24\u4f4d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba9\u6570\u5b57\u663e\u793a\u4e24\u4f4d\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-string\u3001round\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u6700\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>%.2f<\/code>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u663e\u793a\u4e24\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/p>\n<p>formatted_number = &quot;%.2f&quot; % number<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u63a7\u5236\u8f93\u51fa\u683c\u5f0f\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528f-string<\/h3>\n<\/p>\n<p><p>Python 3.6\u5f15\u5165\u4e86f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u4f7f\u7528f-string\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/p>\n<p>formatted_number = f&quot;{number:.2f}&quot;<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>f-string\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u6027\u80fd\u4e0a\u4e5f\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u63a8\u8350\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528round\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>round<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u6570\u5b57\u56db\u820d\u4e94\u5165\u5230\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u867d\u7136\u5b83\u4e0d\u4f1a\u6539\u53d8\u6570\u5b57\u7684\u663e\u793a\u65b9\u5f0f\uff0c\u4f46\u53ef\u4ee5\u914d\u5408\u5176\u4ed6\u65b9\u6cd5\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/p>\n<p>rounded_number = round(number, 2)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528format\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>format<\/code>\u51fd\u6570\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0e\u4e4b\u524d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u8bed\u6cd5\u4e0a\u66f4\u52a0\u7edf\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/p>\n<p>formatted_number = &quot;{:.2f}&quot;.format(number)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5c0f\u6570\u7c7b\uff08decimal\uff09<\/h3>\n<\/p>\n<p><p>Python\u7684<code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Decimal<\/code>\u7c7b\uff0c\u7528\u4e8e\u66f4\u7cbe\u786e\u5730\u8fdb\u884c\u6d6e\u70b9\u8fd0\u7b97\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u5e38\u7528\uff0c\u4f46\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, ROUND_HALF_UP<\/p>\n<p>number = Decimal(&#39;3.14159&#39;)<\/p>\n<p>formatted_number = number.quantize(Decimal(&#39;0.00&#39;), rounding=ROUND_HALF_UP)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5728pandas\u4e2d\u663e\u793a\u4e24\u4f4d\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u4f7f\u7528pandas\u5904\u7406\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.options.display.float_format<\/code>\u6765\u8bbe\u7f6e\u5168\u5c40\u6d6e\u70b9\u6570\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>pd.options.display.float_format = &quot;{:.2f}&quot;.format<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [3.14159, 2.71828, 1.61803]})<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>      A<\/strong><\/h2>\n<h2><strong>0  3.14<\/strong><\/h2>\n<h2><strong>1  2.72<\/strong><\/h2>\n<h2><strong>2  1.62<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5728numpy\u4e2d\u663e\u793a\u4e24\u4f4d\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u5982\u679c\u4f60\u5728\u4f7f\u7528numpy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6253\u5370\u9009\u9879\u6765\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np.set_printoptions(precision=2)<\/p>\n<p>arr = np.array([3.14159, 2.71828, 1.61803])<\/p>\n<p>print(arr)  # \u8f93\u51fa: [3.14 2.72 1.62]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5728matplotlib\u4e2d\u663e\u793a\u4e24\u4f4d\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u4f7f\u7528matplotlib\u7ed8\u5236\u56fe\u5f62\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u523b\u5ea6\u683c\u5f0f\u6765\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3]<\/p>\n<p>y = [3.14159, 2.71828, 1.61803]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.gca().yaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: f&#39;{x:.2f}&#39;))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u7075\u6d3b\u5730\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002\u65e0\u8bba\u662f\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-string\u3001format\u51fd\u6570\uff0c\u8fd8\u662f\u4f7f\u7528\u7279\u5b9a\u5e93\u7684\u8bbe\u7f6e\u9009\u9879\uff0c\u90fd\u80fd\u6ee1\u8db3\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>format()<\/code>\u51fd\u6570\u6216f-string\u6765\u683c\u5f0f\u5316\u6570\u5b57\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>formatted_number = format(your_number, &quot;.2f&quot;)<\/code>\u3002\u5982\u679c\u4f7f\u7528f-string\uff0c\u4ee3\u7801\u5219\u4e3a\uff1a<code>formatted_number = f&quot;{your_number:.2f}&quot;<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u80fd\u6709\u6548\u5730\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u7684\u56db\u820d\u4e94\u5165\uff1f<\/strong><br \/>\u6d6e\u70b9\u6570\u7684\u56db\u820d\u4e94\u5165\u53ef\u4ee5\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8c03\u7528\u65f6\uff0c\u4f20\u5165\u6570\u5b57\u548c\u5e0c\u671b\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\uff0c\u4f8b\u5982\uff1a<code>rounded_number = round(your_number, 2)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684\u6570\u5b57\u5728\u663e\u793a\u65f6\u53ea\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff0c\u5e76\u4e14\u6309\u7167\u56db\u820d\u4e94\u5165\u7684\u89c4\u5219\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5e2e\u52a9\u683c\u5f0f\u5316\u6570\u5b57\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\uff0c<code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u6d6e\u70b9\u6570\u7684\u7cbe\u786e\u63a7\u5236\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8d27\u5e01\u6216\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u60c5\u51b5\u4e0b\u3002\u901a\u8fc7<code>from decimal import Decimal<\/code>\u53ef\u4ee5\u521b\u5efaDecimal\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>quantize()<\/code>\u65b9\u6cd5\u6765\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff1a<code>decimal_number = Decimal(your_number).quantize(Decimal(&#39;0.00&#39;))<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7ed3\u679c\u59cb\u7ec8\u4ee5\u4e24\u4f4d\u5c0f\u6570\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba9\u6570\u5b57\u663e\u793a\u4e24\u4f4d\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001f-string\u3001round\u51fd\u6570\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1080855,"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\/1080847"}],"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=1080847"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080847\/revisions"}],"predecessor-version":[{"id":1080856,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080847\/revisions\/1080856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080855"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}