{"id":1095690,"date":"2025-01-08T14:53:36","date_gmt":"2025-01-08T06:53:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095690.html"},"modified":"2025-01-08T14:53:38","modified_gmt":"2025-01-08T06:53:38","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8d%e6%98%be%e7%a4%ba%e7%a7%91%e5%ad%a6%e8%ae%a1%e6%95%b0%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095690.html","title":{"rendered":"python\u5982\u4f55\u4e0d\u663e\u793a\u79d1\u5b66\u8ba1\u6570\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211233\/0b5c6fc3-ea9d-406e-833b-04ad55644bc6.webp\" alt=\"python\u5982\u4f55\u4e0d\u663e\u793a\u79d1\u5b66\u8ba1\u6570\u6cd5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4e0d\u663e\u793a\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u65b9\u6cd5\u6709\u51e0\u79cd<\/strong>\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u8bbe\u7f6e\u5168\u5c40\u9009\u9879\u3001\u66f4\u6539\u6253\u5370\u9009\u9879\u3002\u5177\u4f53\u6765\u8bf4\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u624b\u52a8\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f<\/strong>\u3002\u8fd9\u662f\u6700\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u6307\u5b9a\u9700\u8981\u7684\u7cbe\u5ea6\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5730\u65b9\u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216\u8005f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u6765\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528format()\u51fd\u6570<\/p>\n<p>num = 0.000123456789<\/p>\n<p>formatted_num = &quot;{:.10f}&quot;.format(num)<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa\uff1a0.0001234568<\/p>\n<h2><strong>\u4f7f\u7528f-string<\/strong><\/h2>\n<p>num = 0.000123456789<\/p>\n<p>formatted_num = f&quot;{num:.10f}&quot;<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa\uff1a0.0001234568<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c<code>{:.10f}<\/code>\u4e2d\u7684<code>.10f<\/code>\u8868\u793a\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u5c0f\u6570\u70b9\u540e10\u4f4d\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u5168\u5c40\u9009\u9879<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\uff0c\u53ef\u4ee5\u8003\u8651\u8bbe\u7f6e\u5168\u5c40\u9009\u9879\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u5168\u5c40\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40\u6d6e\u70b9\u6570\u663e\u793a\u7cbe\u5ea6<\/strong><\/h2>\n<p>np.set_printoptions(suppress=True, precision=10)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>num = np.array([0.000123456789])<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a[0.0001234568]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>np.set_printoptions(suppress=True, precision=10)<\/code>\u8bbe\u7f6e\u4e86NumPy\u6570\u7ec4\u7684\u5168\u5c40\u663e\u793a\u9009\u9879\uff0c<code>suppress=True<\/code>\u7528\u4e8e\u7981\u6b62\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\uff0c<code>precision=10<\/code>\u7528\u4e8e\u63a7\u5236\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u66f4\u6539\u6253\u5370\u9009\u9879<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u63a7\u5236\u6253\u5370\u8f93\u51fa\u7684\u683c\u5f0f\uff0c\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6253\u5370\u51fd\u6570\u6216\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\u6765\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import decimal<\/p>\n<h2><strong>\u4f7f\u7528decimal\u6a21\u5757<\/strong><\/h2>\n<p>num = decimal.Decimal(&#39;0.000123456789&#39;)<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a0.000123456789<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c<code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\uff0c\u5e76\u4e14\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>locale<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>locale<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u8bbe\u7f6e\u533a\u57df\u8bbe\u7f6e\uff0c\u4ece\u800c\u5f71\u54cd\u6570\u5b57\u3001\u65e5\u671f\u548c\u8d27\u5e01\u7684\u663e\u793a\u683c\u5f0f\u3002\u867d\u7136<code>locale<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u56fd\u9645\u5316\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import locale<\/p>\n<h2><strong>\u8bbe\u7f6e\u533a\u57df<\/strong><\/h2>\n<p>locale.setlocale(locale.LC_NUMERIC, &#39;en_US.UTF-8&#39;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>num = 123456789.123456<\/p>\n<p>formatted_num = locale.format_string(&#39;%.10f&#39;, num, grouping=True)<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa\uff1a123,456,789.1234560013<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>locale.format_string(&#39;%.10f&#39;, num, grouping=True)<\/code>\u7528\u4e8e\u683c\u5f0f\u5316\u6570\u5b57\uff0c<code>&#39;%.10f&#39;<\/code>\u6307\u5b9a\u4e86\u5c0f\u6570\u70b9\u540e10\u4f4d\u7684\u7cbe\u5ea6\uff0c<code>grouping=True<\/code>\u7528\u4e8e\u542f\u7528\u5343\u4f4d\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u8868\u683c\u6570\u636e\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u663e\u793a\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>pandas<\/code>\u7684\u663e\u793a\u9009\u9879\u6765\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bbe\u7f6epandas\u663e\u793a\u9009\u9879<\/strong><\/h2>\n<p>pd.set_option(&#39;display.float_format&#39;, lambda x: &#39;%.10f&#39; % x)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [0.000123456789, 0.000987654321]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>pd.set_option(&#39;display.float_format&#39;, lambda x: &#39;%.10f&#39; % x)<\/code>\u8bbe\u7f6e\u4e86<code>pandas<\/code>\u6570\u636e\u5e27\u7684\u5168\u5c40\u663e\u793a\u9009\u9879\uff0c\u4ee5\u663e\u793a\u5c0f\u6570\u70b9\u540e10\u4f4d\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528<code>math<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\uff0c\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u5b83\u6765\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>num = 0.000123456789<\/p>\n<p>formatted_num = f&quot;{math.floor(num * 10&lt;strong&gt;10) \/ 10&lt;\/strong&gt;10:.10f}&quot;<\/p>\n<p>print(formatted_num)  # \u8f93\u51fa\uff1a0.0001234568<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>math.floor(num * 10&lt;strong&gt;10) \/ 10&lt;\/strong&gt;10<\/code>\u7528\u4e8e\u5c06\u6570\u5b57\u622a\u65ad\u5230\u5c0f\u6570\u70b9\u540e10\u4f4d\uff0c\u7136\u540e\u4f7f\u7528f-string\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>fractions<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6709\u7406\u6570\u7684\u8868\u793a\u548c\u8fd0\u7b97\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>num = Fraction(0.000123456789).limit_denominator()<\/p>\n<p>print(num)  # \u8f93\u51fa\uff1a123456789\/1000000000000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Fraction(0.000123456789).limit_denominator()<\/code>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6709\u7406\u6570\u7684\u5f62\u5f0f\uff0c\u5e76\u4e14\u907f\u514d\u4e86\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u663e\u793a\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u8bbe\u7f6e\u5168\u5c40\u9009\u9879\u3001\u66f4\u6539\u6253\u5370\u9009\u9879\u3001\u4f7f\u7528<code>locale<\/code>\u6a21\u5757\u3001<code>pandas<\/code>\u5e93\u3001<code>math<\/code>\u6a21\u5757\u548c<code>fractions<\/code>\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\uff0c\u4ece\u800c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u53ef\u4ee5\u63a7\u5236\u6d6e\u70b9\u6570\u7684\u663e\u793a\u683c\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u6216f-string\uff0c\u53ef\u4ee5\u6307\u5b9a\u7cbe\u786e\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>&quot;{:.2f}&quot;.format(0.000123456)<\/code>\u5c06\u8f93\u51fa<code>0.00<\/code>\uff0c\u800c\u4f7f\u7528f-string\u5219\u53ef\u4ee5\u5199\u6210<code>f&quot;{0.000123456:.2f}&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5e76\u4ee5\u66f4\u76f4\u89c2\u7684\u65b9\u5f0f\u663e\u793a\u6570\u5b57\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Pandas\u65f6\uff0c\u5982\u4f55\u7981\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5728\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u65f6\u9047\u5230\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>pd.set_option(&#39;display.float_format&#39;, &#39;{:.3f}&#39;.format)<\/code>\u6765\u6539\u53d8\u6d6e\u70b9\u6570\u7684\u663e\u793a\u65b9\u5f0f\u3002\u8fd9\u6837\uff0c\u6240\u6709\u7684\u6d6e\u70b9\u6570\u90fd\u5c06\u4ee5\u4f60\u6307\u5b9a\u7684\u683c\u5f0f\u663e\u793a\uff0c\u907f\u514d\u4e86\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u51fa\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728NumPy\u4e2d\u907f\u514d\u8f93\u51fa\u79d1\u5b66\u8ba1\u6570\u6cd5\uff1f<\/strong><br \/>\u5728\u4f7f\u7528NumPy\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6253\u5370\u9009\u9879\u6765\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u3002\u4f7f\u7528<code>np.set_printoptions(suppress=True)<\/code>\u53ef\u4ee5\u7981\u6b62\u4f7f\u7528\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u786e\u4fdd\u6240\u6709\u7684\u6d6e\u70b9\u6570\u90fd\u4ee5\u6b63\u5e38\u7684\u683c\u5f0f\u663e\u793a\u3002\u8fd9\u5bf9\u4e8e\u67e5\u770b\u5927\u578b\u6570\u7ec4\u65f6\u7279\u522b\u6709\u7528\uff0c\u80fd\u591f\u4f7f\u6570\u636e\u7684\u53ef\u8bfb\u6027\u66f4\u9ad8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e0d\u663e\u793a\u79d1\u5b66\u8ba1\u6570\u6cd5\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001\u8bbe\u7f6e\u5168\u5c40\u9009\u9879\u3001\u66f4\u6539\u6253\u5370\u9009\u9879\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1095695,"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\/1095690"}],"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=1095690"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095690\/revisions"}],"predecessor-version":[{"id":1095696,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095690\/revisions\/1095696"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095695"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}