{"id":1094205,"date":"2025-01-08T14:38:50","date_gmt":"2025-01-08T06:38:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094205.html"},"modified":"2025-01-08T14:38:52","modified_gmt":"2025-01-08T06:38:52","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e8%a7%84%e5%ae%9a%e4%bd%8d%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094205.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u636e\u89c4\u5b9a\u4f4d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210429\/c45c99a5-1016-41c1-b300-003ac7a535bc.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u636e\u89c4\u5b9a\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>Python\u5c06\u6570\u636e\u89c4\u5b9a\u4f4d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528round()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001Decimal\u6a21\u5757\u7b49\u3002\u901a\u8fc7\u4f7f\u7528round()\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5904\u7406\u6570\u636e\u65f6\u5e38\u5e38\u9700\u8981\u5bf9\u6570\u5b57\u8fdb\u884c\u89c4\u5b9a\u4f4d\u6570\u7684\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u4e3a\u4e86\u663e\u793a\u7f8e\u89c2\uff0c\u8fd8\u662f\u4e3a\u4e86\u7cbe\u5ea6\u63a7\u5236\uff0c\u6b63\u786e\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u83b7\u5f97\u66f4\u597d\u7684\u6570\u636e\u5904\u7406\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528round()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123.45678<\/p>\n<p>rounded_number = round(number, 2)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 123.46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u6307\u5b9a\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 123.45678<\/p>\n<p>formatted_number = &quot;{:.2f}&quot;.format(number)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 123.46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u6d6e\u70b9\u6570\uff0c\u8fd8\u9002\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\uff0c\u6bd4\u5982\u6574\u6570\u548c\u5b57\u7b26\u4e32\u3002<\/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\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>decimal<\/code>\u6a21\u5757\u3002<code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7cbe\u5ea6\u548c\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, ROUND_HALF_UP<\/p>\n<p>number = Decimal(&#39;123.45678&#39;)<\/p>\n<p>rounded_number = number.quantize(Decimal(&#39;0.01&#39;), rounding=ROUND_HALF_UP)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 123.46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u6216\u975e\u5e38\u5c0f\u7684\u6570\u636e\u65f6\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u662f\u4e00\u79cd\u6709\u6548\u7684\u8868\u793a\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345678<\/p>\n<p>formatted_number = &quot;{:.2e}&quot;.format(number)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 1.23e+07<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5230\u6570\u7ec4\u548c\u77e9\u9635\u7684\u64cd\u4f5c\u65f6\uff0cNumPy\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002NumPy\u63d0\u4f9b\u4e86\u7cbe\u5ea6\u63a7\u5236\u548c\u683c\u5f0f\u5316\u663e\u793a\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([123.45678, 234.56789, 345.67890])<\/p>\n<p>rounded_numbers = np.round(numbers, 2)<\/p>\n<p>print(rounded_numbers)  # \u8f93\u51fa: [123.46 234.57 345.68]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6570\u636e\u6846\u65f6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u63a7\u5236\u6570\u636e\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;numbers&#39;: [123.45678, 234.56789, 345.67890]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;rounded_numbers&#39;] = df[&#39;numbers&#39;].round(2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u793a\u4f8b\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u7cbe\u5ea6\u5bf9\u6570\u5b57\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_round(number, digits):<\/p>\n<p>    factor = 10  digits<\/p>\n<p>    return int(number * factor + 0.5) \/ factor<\/p>\n<p>number = 123.45678<\/p>\n<p>rounded_number = custom_round(number, 2)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 123.46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u6574\u6570\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6574\u6570\u6570\u636e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u63a7\u5236\u5176\u663e\u793a\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5343\u5206\u4f4d\u5206\u9694\u7b26\u6765\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 12345678<\/p>\n<p>formatted_number = &quot;{:,}&quot;.format(number)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 12,345,678<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5927\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6570\u636e\u7684\u89c4\u5b9a\u4f4d\u6570\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c24\u4e3a\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6027\u80fd\u548c\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = {&#39;numbers&#39;: [123.45678] * 1000000}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u8fdb\u884c\u89c4\u5b9a\u4f4d\u6570<\/strong><\/h2>\n<p>df[&#39;rounded_numbers&#39;] = df[&#39;numbers&#39;].round(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5c06\u6570\u636e\u89c4\u5b9a\u4f4d\u6570\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u8fd8\u662f\u7b2c\u4e09\u65b9\u5e93\uff0c\u90fd\u80fd\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\u5b9e\u73b0\u7cbe\u786e\u7684\u63a7\u5236\u3002\u901a\u8fc7\u5b9e\u8df5\u548c\u4e0d\u65ad\u4f18\u5316\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u6700\u9002\u5408\u81ea\u5df1\u9879\u76ee\u7684\u65b9\u6cd5\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\u4fdd\u7559\u7279\u5b9a\u4f4d\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u4fdd\u7559\u7279\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u5904\u7406\u7684\u6d6e\u70b9\u6570\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8981\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>round(3.14159, 2)<\/code>\u5c06\u8fd4\u56de<code>3.14<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u4e5f\u53ef\u4ee5\u5b9e\u73b0\uff0c\u6bd4\u5982<code>&quot;{:.2f}&quot;.format(3.14159)<\/code>\u6216\u8005f\u5b57\u7b26\u4e32<code>f&quot;{3.14159:.2f}&quot;<\/code>\uff0c\u5747\u53ef\u4ee5\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u7279\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5f53\u4f60\u5e0c\u671b\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u7279\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.zfill()<\/code>\u65b9\u6cd5\u6216\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>str(5).zfill(3)<\/code>\u4f1a\u8fd4\u56de<code>&#39;005&#39;<\/code>\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5982<code>&quot;{:03}&quot;.format(5)<\/code>\uff0c\u540c\u6837\u4f1a\u5f97\u5230<code>&#39;005&#39;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5904\u7406\u9700\u8981\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u5b57\u8868\u793a\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u6570\u5b57\u8fdb\u884c\u4f4d\u6570\u89c4\u6574\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>DataFrame.round()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u6570\u636e\u6846\u4e2d\u6570\u5b57\u7684\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>df.round(2)<\/code>\u5c06\u6240\u6709\u6570\u503c\u5217\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u5982\u679c\u53ea\u60f3\u9488\u5bf9\u7279\u5b9a\u5217\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u50cf<code>df[&#39;column_name&#39;].round(2)<\/code>\u8fd9\u6837\u6307\u5b9a\u5217\u540d\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u89c4\u6574\uff0c\u4f7f\u5176\u7b26\u5408\u5206\u6790\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u6570\u636e\u89c4\u5b9a\u4f4d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528round()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001Decimal\u6a21\u5757\u7b49\u3002\u901a\u8fc7\u4f7f\u7528rou [&hellip;]","protected":false},"author":3,"featured_media":1094209,"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\/1094205"}],"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=1094205"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094205\/revisions"}],"predecessor-version":[{"id":1094210,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094205\/revisions\/1094210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094209"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}