{"id":1075557,"date":"2025-01-08T11:45:21","date_gmt":"2025-01-08T03:45:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075557.html"},"modified":"2025-01-08T11:45:24","modified_gmt":"2025-01-08T03:45:24","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e4%bf%9d%e7%95%99%e5%b0%8f%e6%95%b0%e4%bd%8d%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075557.html","title":{"rendered":"python\u5982\u4f55\u5728\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180600\/f9bd7026-7552-44f9-8229-9b7a39528aff.webp\" alt=\"python\u5982\u4f55\u5728\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528round()\u51fd\u6570\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08\u5982f-string\u6216str.format()\uff09\u3001\u4f7f\u7528decimal\u6a21\u5757\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5904\u7406\u6d6e\u70b9\u6570\u65f6\u4fdd\u6301\u7cbe\u5ea6\u548c\u7f8e\u89c2\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528decimal\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528decimal\u6a21\u5757\u53ef\u4ee5\u5728\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u65f6\u4fdd\u6301\u9ad8\u7cbe\u5ea6\uff0c\u907f\u514d\u6d6e\u70b9\u6570\u8fd0\u7b97\u4e2d\u7684\u8bef\u5dee\u3002decimal\u6a21\u5757\u63d0\u4f9b\u4e86Decimal\u7c7b\uff0c\u53ef\u4ee5\u7cbe\u786e\u5730\u8868\u793a\u6d6e\u70b9\u6570\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528round()\u51fd\u6570<\/p>\n<\/p>\n<p><p>round()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u5bf9\u6570\u5b57\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u5b83\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff1a\u9700\u8981\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u7684\u6570\u5b57\u548c\u9700\u8981\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.141592653589793<\/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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6570\u5b573.141592653589793\u56db\u820d\u4e94\u5165\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5c0f\u65703.14\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u5176\u4e2d\u5305\u62ecf-string\u548cstr.format()\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4ee5\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u63a7\u5236\u6570\u5b57\u7684\u663e\u793a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528f-string<\/li>\n<\/ol>\n<p><p>f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u4f7f\u5f97\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.141592653589793<\/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>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528f-string\u5c06\u6570\u5b573.141592653589793\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e323.14\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528str.format()\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>str.format()\u65b9\u6cd5\u662fPython 2.7\u548c3.x\u7248\u672c\u4e2d\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u901a\u8fc7\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u5360\u4f4d\u7b26\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.141592653589793<\/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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528str.format()\u65b9\u6cd5\u5c06\u6570\u5b573.141592653589793\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e323.14\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528decimal\u6a21\u5757<\/p>\n<\/p>\n<p><p>decimal\u6a21\u5757\u63d0\u4f9b\u4e86Decimal\u7c7b\uff0c\u53ef\u4ee5\u7cbe\u786e\u5730\u8868\u793a\u6d6e\u70b9\u6570\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u7cbe\u5ea6\u3002\u8fd9\u5728\u8fdb\u884c\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, getcontext<\/p>\n<h2><strong>\u8bbe\u7f6e\u5168\u5c40\u7cbe\u5ea6<\/strong><\/h2>\n<p>getcontext().prec = 6<\/p>\n<p>number = Decimal(&quot;3.141592653589793&quot;)<\/p>\n<p>rounded_number = number.quantize(Decimal(&quot;1.00&quot;))<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528decimal\u6a21\u5757\u5c06\u6570\u5b573.141592653589793\u7cbe\u786e\u5730\u8868\u793a\u4e3aDecimal\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u91d1\u878d\u6570\u636e\u6216\u8fdb\u884c\u79d1\u5b66\u5b9e\u9a8c\u65f6\u3002\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u8fdb\u884c\u6570\u636e\u6e05\u6d17<\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6570\u636e\u6e05\u6d17\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u89c4\u8303\u6570\u636e\u683c\u5f0f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&quot;values&quot;: [3.141592653589793, 2.718281828459045, 1.618033988749895]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>df[&quot;values&quot;] = df[&quot;values&quot;].round(2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u5e93\u5c06\u6570\u636e\u6846\u4e2d\u7684\u6570\u5b57\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4fdd\u6301\u8ba1\u7b97\u7ed3\u679c\u7684\u7cbe\u5ea6\uff0c\u907f\u514d\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u5bfc\u81f4\u7684\u8ba1\u7b97\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([3.141592653589793, 2.718281828459045, 1.618033988749895])<\/p>\n<h2><strong>\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>rounded_array = np.round(array, 2)<\/p>\n<p>print(rounded_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528numpy\u5e93\u5c06\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528round()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff08\u5982f-string\u6216str.format()\uff09\u3001\u4ee5\u53cadecimal\u6a21\u5757\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4fdd\u6301\u8ba1\u7b97\u7ed3\u679c\u7684\u7cbe\u5ea6\uff0c\u907f\u514d\u7531\u4e8e\u820d\u5165\u8bef\u5dee\u5bfc\u81f4\u7684\u8ba1\u7b97\u9519\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u5c0f\u6570\u4ee5\u4fdd\u7559\u7279\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>format()<\/code>\u51fd\u6570\u6216\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u4fdd\u7559\u7279\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>f&quot;{value:.2f}&quot;<\/code>\u53ef\u4ee5\u5c06<code>value<\/code>\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\u3002\u53e6\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u95ee\u9898\uff1f<\/strong><br \/>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u4e2d\u53ef\u80fd\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u3002\u4f7f\u7528<code>Decimal<\/code>\u7c7b\u53ef\u4ee5\u521b\u5efa\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\uff0c\u786e\u4fdd\u5728\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u4e0d\u4f1a\u51fa\u73b0\u610f\u5916\u7684\u7cbe\u5ea6\u635f\u5931\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u4ee5\u4fbf\u4e8e\u663e\u793a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u6216<code>format()<\/code>\u51fd\u6570\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5982\u679c\u60f3\u8981\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee5\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>f-string<\/code>\u6216<code>format()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c<code>f&quot;{value:.3f}&quot;<\/code>\u53ef\u4ee5\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u3002\u8fd9\u5728\u751f\u6210\u62a5\u544a\u6216\u8f93\u51fa\u7ed3\u679c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528round [&hellip;]","protected":false},"author":3,"featured_media":1075569,"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\/1075557"}],"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=1075557"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075557\/revisions"}],"predecessor-version":[{"id":1075570,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075557\/revisions\/1075570"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075569"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}