{"id":1145012,"date":"2025-01-08T23:04:40","date_gmt":"2025-01-08T15:04:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1145012.html"},"modified":"2025-01-08T23:04:43","modified_gmt":"2025-01-08T15:04:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e5%8f%96%e5%b0%8f%e6%95%b0%e7%82%b9%e5%90%8e%e4%b8%a4%e4%bd%8d%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145012.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\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\/24181824\/2687e813-294d-45e7-988d-17b97f50e689.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u5c0f\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u5c0f\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001round()\u51fd\u6570\u3001decimal\u6a21\u5757\u3001\u548cmath\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6d6e\u70b9\u6570\u9650\u5236\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>:.2f<\/code>\u683c\u5f0f\u8bf4\u660e\u7b26\uff0c\u4f8b\u5982<code>&quot;{:.2f}&quot;.format(3.14159)<\/code>\u5c06\u8f93\u51fa<code>3.14<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u4fbf\uff0c\u8fd8\u80fd\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u56db\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u662fPython\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u5230\u7279\u5b9a\u5c0f\u6570\u4f4d\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/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><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>:.2f<\/code>\u8bf4\u660e\u7b26\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u9650\u5236\u4e3a\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u4fbf\uff0c\u8fd8\u80fd\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u663e\u793a\u7684\u573a\u666f\uff0c\u6bd4\u5982\u751f\u6210\u62a5\u544a\u3001\u8f93\u51fa\u65e5\u5fd7\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001round()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u6570\u5b57\u56db\u820d\u4e94\u5165\u5230\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/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><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u7528\u4e8e\u8ba1\u7b97\u548c\u6570\u5b66\u8fd0\u7b97\u4e2d\u3002\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u662f\u9700\u8981\u5904\u7406\u7684\u6570\u5b57\uff0c\u53e6\u4e00\u4e2a\u662f\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>round()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u3002\u5982\u679c\u9700\u8981\u8fdb\u4e00\u6b65\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u53ef\u4ee5\u7ed3\u5408\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u65b9\u6cd5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001decimal\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6d6e\u70b9\u6570\u8fd0\u7b97\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u7cbe\u786e\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u7684\u91d1\u878d\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, ROUND_HALF_UP<\/p>\n<p>number = Decimal(&#39;3.14159&#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: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><code>decimal<\/code>\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u4ee5\u66f4\u9ad8\u7684\u7cbe\u5ea6\u8fdb\u884c\u6d6e\u70b9\u6570\u8fd0\u7b97\uff0c\u907f\u514d\u4e86\u6d6e\u70b9\u6570\u8fd0\u7b97\u4e2d\u7684\u7cbe\u5ea6\u95ee\u9898\u3002\u901a\u8fc7\u8bbe\u7f6e<code>ROUND_HALF_UP<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u64cd\u4f5c\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u7cbe\u5ea6\u6709\u9ad8\u8981\u6c42\u7684\u573a\u666f\uff0c\u5982\u91d1\u878d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001math\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>math<\/code>\u6a21\u5757\u6ca1\u6709\u76f4\u63a5\u7528\u4e8e\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u5b66\u8fd0\u7b97\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4e0e\u5176\u4ed6\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>number = 3.14159<\/p>\n<p>rounded_number = math.floor(number * 100) \/ 100<\/p>\n<p>print(rounded_number)  # \u8f93\u51fa: 3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5c06\u6570\u5b57\u4e58\u4ee5100\uff0c\u4f7f\u7528<code>math.floor()<\/code>\u51fd\u6570\u53d6\u6574\uff0c\u7136\u540e\u518d\u9664\u4ee5100\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u7684\u6548\u679c\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\u5316\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong>\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u9700\u8981\u8f93\u51fa\u548c\u663e\u793a\u7684\u573a\u666f\uff0c<strong>round()\u51fd\u6570<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\uff0c<strong>decimal\u6a21\u5757<\/strong>\u9002\u7528\u4e8e\u9ad8\u7cbe\u5ea6\u8981\u6c42\u7684\u91d1\u878d\u548c\u79d1\u5b66\u8ba1\u7b97\uff0c\u800c<strong>math\u6a21\u5757<\/strong>\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u7075\u6d3b\u5904\u7406\u6d6e\u70b9\u6570\u7684\u5c0f\u6570\u4f4d\u6570\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u56db\u820d\u4e94\u5165\u4e00\u4e2a\u6d6e\u70b9\u6570\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u3002\u5177\u4f53\u505a\u6cd5\u662f\u5c06\u76ee\u6807\u6d6e\u70b9\u6570\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a2\u3002\u4f8b\u5982\uff0c<code>round(3.14159, 2)<\/code>\u5c06\u8fd4\u56de<code>3.14<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u5408\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u4ee5\u663e\u793a\u4e24\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u683c\u5f0f\u5316\u6d6e\u70b9\u6570\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>f-string<\/code>\u6216\u8005<code>format()<\/code>\u65b9\u6cd5\u5747\u53ef\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>f-string<\/code>\u65f6\uff0c\u53ef\u4ee5\u5199\u6210<code>f&quot;{value:.2f}&quot;<\/code>\uff0c\u5b83\u4f1a\u5c06<code>value<\/code>\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u5c0f\u6570\u3002\u4f7f\u7528<code>format()<\/code>\u65b9\u6cd5\u5219\u53ef\u4ee5\u5199\u6210<code>&quot;{:.2f}&quot;.format(value)<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u591f\u786e\u4fdd\u8f93\u51fa\u7ed3\u679c\u59cb\u7ec8\u662f\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u7684\u6d6e\u70b9\u6570\u4ee5\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5904\u7406\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6d6e\u70b9\u6570\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u904d\u5386\u5217\u8868\u5e76\u683c\u5f0f\u5316\u6bcf\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>rounded_list = [round(num, 2) for num in original_list]<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6d6e\u70b9\u6570\u90fd\u88ab\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u8fd9\u79cd\u65b9\u5f0f\u9ad8\u6548\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u6279\u91cf\u5904\u7406\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53d6\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u5c0f\u6570\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001round()\u51fd\u6570\u3001decimal\u6a21\u5757\u3001\u548cm [&hellip;]","protected":false},"author":3,"featured_media":1145019,"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\/1145012"}],"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=1145012"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145012\/revisions"}],"predecessor-version":[{"id":1145024,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145012\/revisions\/1145024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145019"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}