{"id":1100885,"date":"2025-01-08T15:44:08","date_gmt":"2025-01-08T07:44:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100885.html"},"modified":"2025-01-08T15:44:11","modified_gmt":"2025-01-08T07:44:11","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bf%9d%e7%95%99%e4%b8%89%e4%bd%8d%e5%b0%8f%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100885.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063912\/574af5e6-e21f-4ce9-ab70-fd35767b12e8.webp\" alt=\"python\u4e2d\u5982\u4f55\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001round()\u51fd\u6570\u3001Decimal\u6a21\u5757\u3001\u4ee5\u53caf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u662fPython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7ed3\u679c\u6e05\u6670\u53ef\u8bfb\uff0c\u5e76\u4e14\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.1415926<\/p>\n<p>formatted_number = f&quot;{number:.3f}&quot;<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e00\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u7cbe\u5ea6\u95ee\u9898\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u4e0d\u540c\u7684\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u5b83\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728Python 3.6\u4e4b\u524d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u4f46\u5728\u73b0\u4ee3Python\u4e2d\u5df2\u4e0d\u63a8\u8350\u4f7f\u7528\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.1415926<\/p>\n<p>formatted_number = &quot;%.3f&quot; % number<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8ePython 2\u548cPython 3\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u53ef\u8bfb\u6027\u4e0d\u4f73\uff0c\u4e0d\u63a8\u8350\u5728\u65b0\u4ee3\u7801\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528str.format()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728Python 3.0\u5f15\u5165\uff0c\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.1415926<\/p>\n<p>formatted_number = &quot;{:.3f}&quot;.format(number)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u6bd4\u767e\u5206\u53f7\u683c\u5f0f\u5316\u66f4\u7075\u6d3b\uff0c\u9002\u7528\u4e8ePython 3\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u8bed\u6cd5\u7a0d\u663e\u7e41\u7410\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528round()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>round()\u51fd\u6570\u7528\u4e8e\u5bf9\u6570\u5b57\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff0c\u8fd4\u56de\u4fdd\u7559\u6307\u5b9a\u5c0f\u6570\u4f4d\u6570\u7684\u6d6e\u70b9\u6570\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.1415926<\/p>\n<p>rounded_number = round(number, 3)<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u8bed\u6cd5\u7b80\u5355\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u7ed3\u679c\u662f\u6d6e\u70b9\u6570\uff0c\u6709\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Decimal\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Decimal\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u5341\u8fdb\u5236\u6d6e\u70b9\u6570\u7684\u66f4\u9ad8\u7cbe\u5ea6\u63a7\u5236\uff0c\u9002\u7528\u4e8e\u5bf9\u7cbe\u5ea6\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/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.1415926&quot;)<\/p>\n<p>formatted_number = round(number, 3)<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9ad8\u7cbe\u5ea6\u63a7\u5236\uff0c\u9002\u7528\u4e8e\u91d1\u878d\u8ba1\u7b97\u7b49\u5bf9\u7cbe\u5ea6\u8981\u6c42\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u8bed\u6cd5\u590d\u6742\uff0c\u6027\u80fd\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff09<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u5f15\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b0\u7279\u6027\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.1415926<\/p>\n<p>formatted_number = f&quot;{number:.3f}&quot;<\/p>\n<p>print(formatted_number)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u65e5\u5e38\u4f7f\u7528\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u4e14\u9002\u5408\u73b0\u4ee3Python\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n<\/p>\n<table>\n<thead>\n<tr>\n<p><th>\u65b9\u6cd5<\/th>\n<\/p>\n<p><th>\u4f18\u70b9<\/th>\n<\/p>\n<p><th>\u7f3a\u70b9<\/th>\n<\/p>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<p><td>\u767e\u5206\u53f7\u683c\u5f0f\u5316<\/td>\n<\/p>\n<p><td>\u7b80\u5355\u6613\u7528\uff0c\u517c\u5bb9\u6027\u597d<\/td>\n<\/p>\n<p><td>\u53ef\u8bfb\u6027\u5dee\uff0c\u4e0d\u63a8\u8350\u65b0\u4ee3\u7801\u4f7f\u7528<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>str.format()<\/td>\n<\/p>\n<p><td>\u7075\u6d3b\uff0c\u9002\u7528\u4e8ePython 3<\/td>\n<\/p>\n<p><td>\u8bed\u6cd5\u7e41\u7410<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>round()<\/td>\n<\/p>\n<p><td>\u8bed\u6cd5\u7b80\u5355<\/td>\n<\/p>\n<p><td>\u53ef\u80fd\u51fa\u73b0\u7cbe\u5ea6\u95ee\u9898<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>Decimal\u6a21\u5757<\/td>\n<\/p>\n<p><td>\u9ad8\u7cbe\u5ea6\u63a7\u5236<\/td>\n<\/p>\n<p><td>\u8bed\u6cd5\u590d\u6742\uff0c\u6027\u80fd\u8f83\u4f4e<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>f-string<\/td>\n<\/p>\n<p><td>\u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u73b0\u4ee3Python\u6700\u4f73\u5b9e\u8df5<\/td>\n<\/p>\n<p><td>\u4ec5\u9002\u7528\u4e8ePython 3.6\u53ca\u4ee5\u4e0a\u7248\u672c<\/td>\n<\/p>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u6765\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u91d1\u878d\u8ba1\u7b97\u65f6\uff0c\u5e94\u8be5\u9009\u62e9Decimal\u6a21\u5757\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7cbe\u5ea6\uff1b\u5728\u7f16\u5199\u73b0\u4ee3Python\u4ee3\u7801\u65f6\uff0c\u5e94\u8be5\u4f18\u5148\u9009\u62e9f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4ee5\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6df1\u5165\u7406\u89e3Decimal\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Decimal\u6a21\u5757\u5728\u5904\u7406\u6d89\u53ca\u91d1\u94b1\u6216\u5176\u4ed6\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u6570\u503c\u8ba1\u7b97\u65f6\u5c24\u4e3a\u91cd\u8981\u3002\u5b83\u63d0\u4f9b\u4e86\u6bd4\u5185\u7f6e\u6d6e\u70b9\u6570\u66f4\u9ad8\u7684\u7cbe\u5ea6\u548c\u66f4\u597d\u7684\u63a7\u5236\u3002\u4e0b\u9762\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8Decimal\u6a21\u5757\u7684\u7528\u6cd5\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u521b\u5efaDecimal\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u6216\u5143\u7ec4\u6765\u521b\u5efaDecimal\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>number = Decimal(&quot;3.1415926&quot;)<\/p>\n<p>print(number)  # \u8f93\u51fa: 3.1415926<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u7b26\u4e32\u521b\u5efaDecimal\u5bf9\u8c61\u53ef\u4ee5\u907f\u514d\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u63a7\u5236\u7cbe\u5ea6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528getcontext()\u548csetcontext()\u51fd\u6570\u6765\u8bbe\u7f6e\u5168\u5c40\u7cbe\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import getcontext<\/p>\n<p>getcontext().prec = 6<\/p>\n<p>number = Decimal(&quot;3.1415926&quot;)<\/p>\n<p>print(number)  # \u8f93\u51fa: 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u7b97\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>6.3 \u6570\u5b66\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>Decimal\u6a21\u5757\u652f\u6301\u5e38\u89c1\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5e76\u4e14\u80fd\u591f\u4fdd\u6301\u9ad8\u7cbe\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>a = Decimal(&quot;1.1&quot;)<\/p>\n<p>b = Decimal(&quot;2.2&quot;)<\/p>\n<p>result = a + b<\/p>\n<p>print(result)  # \u8f93\u51fa: 3.3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7cbe\u5ea6\u63a7\u5236\u5728\u91d1\u878d\u8ba1\u7b97\u4e2d\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u4efb\u4f55\u7ec6\u5c0f\u7684\u8bef\u5dee\u90fd\u53ef\u80fd\u5bfc\u81f4\u4e25\u91cd\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u8df5\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4e86\u89e3\u5982\u4f55\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u540c\u6837\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u683c\u5f0f\u5316\u548c\u4fdd\u7559\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u7ed3\u679c\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u4ee5\u4fbf\u4e8e\u5c55\u793a\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [3.1415926, 2.7182818, 1.6180339]<\/p>\n<p>formatted_data = [f&quot;{num:.3f}&quot; for num in data]<\/p>\n<p>print(formatted_data)  # \u8f93\u51fa: [&#39;3.142&#39;, &#39;2.718&#39;, &#39;1.618&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u91d1\u878d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u8ba1\u7b97\u4e2d\uff0c\u4f7f\u7528Decimal\u6a21\u5757\u53ef\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u7cbe\u5ea6\uff0c\u4ece\u800c\u907f\u514d\u56e0\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u5bfc\u81f4\u7684\u8ba1\u7b97\u8bef\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal<\/p>\n<p>amount = Decimal(&quot;1234.5678&quot;)<\/p>\n<p>interest_rate = Decimal(&quot;0.05&quot;)<\/p>\n<p>interest = amount * interest_rate<\/p>\n<p>print(interest)  # \u8f93\u51fa: 61.72839<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.3 \u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u7ed3\u679c\u8fdb\u884c\u683c\u5f0f\u5316\u4ee5\u4fbf\u4e8e\u9605\u8bfb\u548c\u6bd4\u8f83\u3002\u53ef\u4ee5\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = 3.1415926<\/p>\n<p>formatted_result = f&quot;{result:.3f}&quot;<\/p>\n<p>print(formatted_result)  # \u8f93\u51fa: 3.142<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6700\u4f73\u5b9e\u8df5\u4e0e\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9664\u4e86\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u548c\u6027\u80fd\u4f18\u5316\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u4f18\u5148\u4f7f\u7528f-string<\/h4>\n<\/p>\n<p><p>\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u4f18\u5148\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u5e76\u4e14\u6027\u80fd\u4f18\u4e8estr.format()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7cbe\u5ea6\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Decimal\u6a21\u5757\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u8bbe\u7f6e\u8fc7\u9ad8\u7684\u7cbe\u5ea6\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u53ea\u5728\u5fc5\u8981\u65f6\u8c03\u6574\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>8.3 \u6279\u91cf\u5904\u7406\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [3.1415926, 2.7182818, 1.6180339]<\/p>\n<p>formatted_data = [f&quot;{num:.3f}&quot; for num in data]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.4 \u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u65f6\uff0c\u4f7f\u7528Decimal\u6a21\u5757\uff0c\u800c\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u63a8\u8350\u4f7f\u7528f-string\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u3001\u76f4\u89c2\uff0c\u5e76\u4e14\u6027\u80fd\u4f18\u8d8a\u3002\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Decimal\u6a21\u5757\u3002\u901a\u8fc7\u7406\u89e3\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u53ef\u9760\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u8981\u7262\u8bb0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u7cbe\u5ea6\u548c\u6027\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u6574\u4f53\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u4ee5\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>format()<\/code>\u51fd\u6570\u6216f-string\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6d6e\u70b9\u6570\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>&quot;{:.3f}&quot;.format(number)<\/code>\u6216<code>f&quot;{number:.3f}&quot;<\/code>\u90fd\u80fd\u8fbe\u5230\u76ee\u7684\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5747\u80fd\u591f\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5448\u73b0\uff0c\u4e14\u5c0f\u6570\u70b9\u540e\u6709\u4e09\u4f4d\u6570\u5b57\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u4f1a\u5f71\u54cd\u6d6e\u70b9\u6570\u7684\u8ba1\u7b97\u5417\uff1f<\/strong><br \/>\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u4ec5\u5728\u8f93\u51fa\u65f6\u5f71\u54cd\u663e\u793a\u7ed3\u679c\uff0c\u5b9e\u9645\u8ba1\u7b97\u4ecd\u7136\u4f7f\u7528\u539f\u59cb\u7684\u6d6e\u70b9\u6570\u503c\u3002\u82e5\u9700\u8981\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u9650\u5236\u5c0f\u6570\u4f4d\u6570\uff0c\u5efa\u8bae\u5728\u6bcf\u6b21\u8ba1\u7b97\u540e\u4f7f\u7528\u76f8\u5e94\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u4ee5\u4fdd\u8bc1\u6570\u503c\u7684\u4e00\u81f4\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684round()\u51fd\u6570\u5982\u4f55\u5b9e\u73b0\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u529f\u80fd\uff1f<\/strong><br \/><code>round()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u6d6e\u70b9\u6570\u7684\u56db\u820d\u4e94\u5165\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>round(number, 3)<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u6d6e\u70b9\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>round()<\/code>\u8fd4\u56de\u7684\u7ed3\u679c\u4ecd\u7136\u662f\u6d6e\u70b9\u6570\uff0c\u5982\u679c\u9700\u8981\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8f93\u51fa\uff0c\u53ef\u4ee5\u7ed3\u5408<code>format()<\/code>\u6216f-string\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fdd\u7559\u4e09\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001round()\u51fd\u6570\u3001Decimal\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1100898,"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\/1100885"}],"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=1100885"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100885\/revisions"}],"predecessor-version":[{"id":1100899,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100885\/revisions\/1100899"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100898"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}