{"id":1085995,"date":"2025-01-08T13:21:27","date_gmt":"2025-01-08T05:21:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085995.html"},"modified":"2025-01-08T13:21:29","modified_gmt":"2025-01-08T05:21:29","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98%e5%b0%8f%e6%95%b0%e4%bd%8d%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085995.html","title":{"rendered":"python\u5982\u4f55\u6539\u53d8\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\/24195624\/4972e659-45b4-47a1-b68f-49735a398680.webp\" alt=\"python\u5982\u4f55\u6539\u53d8\u5c0f\u6570\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u6539\u53d8\u5c0f\u6570\u7684\u4f4d\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684round()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u4ee5\u53caDecimal\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528round()\u51fd\u6570<\/strong>\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u53ea\u80fd\u5bf9\u5c0f\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u82e5\u9700\u8981\u66f4\u7cbe\u786e\u7684\u63a7\u5236\uff0c\u4f8b\u5982\u907f\u514d\u56db\u820d\u4e94\u5165\u6216\u8005\u9700\u8981\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>Decimal\u6a21\u5757<\/strong>\u3002\u4e3a\u4e86\u5b9e\u73b0\u7279\u5b9a\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u6539\u53d8\u5c0f\u6570\u7684\u4f4d\u6570\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\uff0c\u53ef\u4ee5\u7528\u6765\u5bf9\u6d6e\u70b9\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u9700\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\uff0c\u7b2c\u4e8c\u4e2a\u662f\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>number = 3.1415926535<\/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>\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u6570\u503c\u5904\u7406\u9700\u6c42\u3002\u7136\u800c\uff0cround()\u51fd\u6570\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u9700\u8981\u5bf9\u5c0f\u6570\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u5904\u7406\uff0c\u53ef\u80fd\u9700\u8981\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5c0f\u6570\u7684\u663e\u793a\u4f4d\u6570\u3002\u6700\u5e38\u7528\u7684\u6709f-string\u548cformat()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528f-string\u683c\u5f0f\u5316<\/li>\n<\/ol>\n<p><p>f-string\u662f\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4ecePython 3.6\u5f00\u59cb\u5f15\u5165\u3002\u5b83\u5141\u8bb8\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>number = 3.1415926535<\/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<ol start=\"2\">\n<li>\u4f7f\u7528format()\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>format()\u65b9\u6cd5\u662f\u53e6\u4e00\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>number = 3.1415926535<\/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>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u63a7\u5236\u5c0f\u6570\u7684\u663e\u793a\u4f4d\u6570\uff0c\u8fd8\u80fd\u8fdb\u884c\u66f4\u591a\u7684\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u4f8b\u5982\u63a7\u5236\u6570\u5b57\u7684\u5bf9\u9f50\u65b9\u5f0f\u3001\u586b\u5145\u5b57\u7b26\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Decimal\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\uff0c\u907f\u514d\u6d6e\u70b9\u6570\u8ba1\u7b97\u4e2d\u7684\u7cbe\u5ea6\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Decimal\u6a21\u5757\u3002Decimal\u6a21\u5757\u63d0\u4f9b\u4e86\u5341\u8fdb\u5236\u6d6e\u70b9\u6570\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u7cbe\u786e\u5730\u8868\u793a\u548c\u5904\u7406\u5c0f\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165Decimal\u6a21\u5757\u5e76\u8bbe\u7f6e\u7cbe\u5ea6<\/li>\n<\/ol>\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 = 10<\/p>\n<h2><strong>\u521b\u5efaDecimal\u5bf9\u8c61<\/strong><\/h2>\n<p>number = Decimal(&#39;3.1415926535&#39;)<\/p>\n<h2><strong>\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>rounded_number = number.quantize(Decimal(&#39;0.01&#39;))<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u63a7\u5236\u4e0d\u540c\u7684\u56db\u820d\u4e94\u5165\u65b9\u5f0f<\/li>\n<\/ol>\n<p><p>Decimal\u6a21\u5757\u8fd8\u5141\u8bb8\u4f60\u63a7\u5236\u4e0d\u540c\u7684\u56db\u820d\u4e94\u5165\u65b9\u5f0f\uff0c\u4f8b\u5982\u5411\u4e0a\u53d6\u6574\u3001\u5411\u4e0b\u53d6\u6574\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, ROUND_DOWN, ROUND_UP<\/p>\n<p>number = Decimal(&#39;3.1415926535&#39;)<\/p>\n<h2><strong>\u5411\u4e0b\u53d6\u6574<\/strong><\/h2>\n<p>rounded_down = number.quantize(Decimal(&#39;0.01&#39;), rounding=ROUND_DOWN)<\/p>\n<p>print(rounded_down)  # \u8f93\u51fa: 3.14<\/p>\n<h2><strong>\u5411\u4e0a\u53d6\u6574<\/strong><\/h2>\n<p>rounded_up = number.quantize(Decimal(&#39;0.01&#39;), rounding=ROUND_UP)<\/p>\n<p>print(rounded_up)  # \u8f93\u51fa: 3.15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u7cbe\u786e\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u548c\u56db\u820d\u4e94\u5165\u65b9\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Numpy\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u65f6\uff0c\u5e38\u5e38\u4f1a\u4f7f\u7528Numpy\u5e93\u3002Numpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u6570\u503c\u5904\u7406\u529f\u80fd\uff0c\u5305\u62ec\u6539\u53d8\u5c0f\u6570\u4f4d\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>number = 3.1415926535<\/p>\n<p>rounded_number = np.round(number, 2)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Numpy\u7684round\u51fd\u6570\u7c7b\u4f3c\u4e8ePython\u5185\u7f6e\u7684round()\u51fd\u6570\uff0c\u4f46\u5b83\u53ef\u4ee5\u5904\u7406Numpy\u6570\u7ec4\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<ol>\n<li><strong>round()\u51fd\u6570<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u4f46\u53ea\u80fd\u8fdb\u884c\u56db\u820d\u4e94\u5165\u64cd\u4f5c\uff0c\u9002\u5408\u4e00\u822c\u7684\u6570\u503c\u5904\u7406\u9700\u6c42\u3002<\/li>\n<li><strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/strong>\uff1a\u7075\u6d3b\u4e14\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u7279\u5b9a\u683c\u5f0f\u8f93\u51fa\u7684\u573a\u666f\u3002<\/li>\n<li><strong>Decimal\u6a21\u5757<\/strong>\uff1a\u63d0\u4f9b\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u548c\u591a\u79cd\u56db\u820d\u4e94\u5165\u65b9\u5f0f\uff0c\u9002\u5408\u9700\u8981\u7cbe\u786e\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u548c\u5904\u7406\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/li>\n<li><strong>Numpy\u5e93<\/strong>\uff1a\u9002\u5408\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\uff0c\u63d0\u4f9b\u4e0eNumpy\u6570\u7ec4\u517c\u5bb9\u7684\u6570\u503c\u5904\u7406\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u6539\u53d8\u5c0f\u6570\u7684\u4f4d\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u6570\u503c\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528round()\u51fd\u6570\u6216\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u9700\u8981\u7cbe\u786e\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u7684\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528Decimal\u6a21\u5757\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u79d1\u5b66\u8ba1\u7b97\uff0cNumpy\u5e93\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u573a\u666f\u4e2d\u7075\u6d3b\u5904\u7406\u5c0f\u6570\u4f4d\u6570\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u51c6\u786e\u6027\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\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\u8c03\u6574\u5c0f\u6570\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>format()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<code>formatted_value = format(value, &#39;.2f&#39;)<\/code>\uff0c\u8fd9\u4f1a\u5c06\u6570\u503c\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002\u4f7f\u7528f-string\u683c\u5f0f\u5316\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\uff0c\u5982\uff1a<code>formatted_value = f&quot;{value:.2f}&quot;<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86<code>round()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5bf9\u5c0f\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u4f7f\u7528\u65b9\u6cd5\u4e3a<code>rounded_value = round(value, n)<\/code>\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u5e0c\u671b\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>round(3.14159, 2)<\/code>\u4f1a\u8fd4\u56de<code>3.14<\/code>\u3002\u8fd9\u4e2a\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6574\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Pandas\u4e2d\u6539\u53d8\u5c0f\u6570\u4f4d\u6570\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5728\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.round()<\/code>\u65b9\u6cd5\u6765\u6539\u53d8\u5c0f\u6570\u4f4d\u6570\u3002\u4f8b\u5982\uff0c<code>df[&#39;column_name&#39;] = df[&#39;column_name&#39;].round(2)<\/code>\u4f1a\u5c06\u6307\u5b9a\u5217\u7684\u5c0f\u6570\u4f4d\u6570\u6539\u4e3a\u4e24\u4f4d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u5904\u7406\u6570\u636e\u65f6\u4fdd\u6301\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u6539\u53d8\u5c0f\u6570\u7684\u4f4d\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684round()\u51fd\u6570\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u4ee5\u53caDecim [&hellip;]","protected":false},"author":3,"featured_media":1086005,"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\/1085995"}],"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=1085995"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085995\/revisions"}],"predecessor-version":[{"id":1086007,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085995\/revisions\/1086007"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1086005"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}