{"id":1125394,"date":"2025-01-08T19:51:31","date_gmt":"2025-01-08T11:51:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1125394.html"},"modified":"2025-01-08T19:51:34","modified_gmt":"2025-01-08T11:51:34","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e5%9b%9b%e8%88%8d%e4%ba%94%e5%85%a5%e7%9a%84%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1125394.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u56db\u820d\u4e94\u5165\u7684\u7ed3\u679c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090105\/27689663-1053-4347-b3c7-e8d851390885.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u56db\u820d\u4e94\u5165\u7684\u7ed3\u679c\" \/><\/p>\n<p><p> <strong>\u56db\u820d\u4e94\u5165\u662f\u6211\u4eec\u5728\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\u5e38\u5e38\u9047\u5230\u7684\u95ee\u9898\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u56db\u820d\u4e94\u5165\uff0c\u5305\u62ec\u5185\u7f6e\u51fd\u6570\u3001\u5e93\u51fd\u6570\u548c\u624b\u52a8\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u3001<code>math<\/code>\u5e93\u4e2d\u7684<code>math.ceil()<\/code>\u548c<code>math.floor()<\/code>\u51fd\u6570\uff0c\u4ee5\u53ca\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b9e\u73b0\u7684\u56db\u820d\u4e94\u5165\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4e00\u4e9b\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001ROUND()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u56db\u820d\u4e94\u5165\u65b9\u6cd5\u3002\u5b83\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f20\u5165\u9700\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\u548c\u53ef\u9009\u7684\u5c0f\u6570\u4f4d\u6570\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rounded_number = round(3.14159, 2)  # \u7ed3\u679c\u662f3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u8981\u5904\u7406\u7684\u6570\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8981\u4fdd\u7559\u7684\u5c0f\u6570\u4f4d\u6570\uff08\u9ed8\u8ba4\u4e3a0\uff09\u3002\u5f53\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a0\u65f6\uff0c<code>round()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(round(3.14159))  # \u7ed3\u679c\u662f3<\/p>\n<p>print(round(3.14159, 2))  # \u7ed3\u679c\u662f3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8d1f\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>round()<\/code>\u51fd\u6570\u540c\u6837\u9002\u7528\u4e8e\u8d1f\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(round(-3.14159, 2))  # \u7ed3\u679c\u662f-3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u56db\u820d\u4e94\u5165\u89c4\u5219<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python 3\u4e2d\uff0c<code>round()<\/code>\u51fd\u6570\u4f7f\u7528\u7684\u662f\u201c\u56db\u820d\u516d\u5165\u4e94\u6210\u53cc\u201d\u7684\u89c4\u5219\uff0c\u5373\u5982\u679c\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\u6b63\u597d\u5728\u4e24\u4e2a\u6570\u7684\u4e2d\u95f4\uff0c<code>round()<\/code>\u4f1a\u9009\u62e9\u4f7f\u7ed3\u679c\u4e3a\u5076\u6570\u7684\u90a3\u4e2a\u65b9\u5411\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(round(2.5))  # \u7ed3\u679c\u662f2<\/p>\n<p>print(round(3.5))  # \u7ed3\u679c\u662f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001MATH\u5e93\u4e2d\u7684CEIL()\u548cFLOOR()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9664\u4e86<code>round()<\/code>\u51fd\u6570\uff0cPython\u7684<code>math<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86<code>math.ceil()<\/code>\u548c<code>math.floor()<\/code>\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u5411\u4e0a\u53d6\u6574\u548c\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>math.ceil()<\/strong><\/p>\n<\/p>\n<p><p><code>math.ceil()<\/code>\u51fd\u6570\u8fd4\u56de\u5927\u4e8e\u7b49\u4e8e\u8f93\u5165\u503c\u7684\u6700\u5c0f\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.ceil(3.14159))  # \u7ed3\u679c\u662f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>math.floor()<\/strong><\/p>\n<\/p>\n<p><p><code>math.floor()<\/code>\u51fd\u6570\u8fd4\u56de\u5c0f\u4e8e\u7b49\u4e8e\u8f93\u5165\u503c\u7684\u6700\u5927\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.floor(3.14159))  # \u7ed3\u679c\u662f3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u56db\u820d\u4e94\u5165<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u9700\u8981\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u5c55\u793a\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u6765\u5b9e\u73b0\u56db\u820d\u4e94\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_number = &quot;{:.2f}&quot;.format(3.14159)  # \u7ed3\u679c\u662f&#39;3.14&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>f-string<\/strong><\/p>\n<\/p>\n<p><p>\u4ecePython 3.6\u5f00\u59cb\uff0cf-string\u6210\u4e3a\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 3.14159<\/p>\n<p>formatted_number = f&quot;{number:.2f}&quot;  # \u7ed3\u679c\u662f&#39;3.14&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u624b\u52a8\u5b9e\u73b0\u56db\u820d\u4e94\u5165<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u9700\u6c42\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u7b97\u6cd5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7b97\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u624b\u52a8\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u7684\u57fa\u672c\u601d\u8def\u662f\u5c06\u6570\u5b57\u4e58\u4ee510\u7684\u5e42\u6b21\u65b9\uff0c\u8fdb\u884c\u52a00.5\u540e\u53d6\u6574\uff0c\u518d\u9664\u4ee510\u7684\u5e42\u6b21\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_round(number, decimals=0):<\/p>\n<p>    factor = 10  decimals<\/p>\n<p>    return int(number * factor + 0.5) \/ factor<\/p>\n<p>print(custom_round(3.14159, 2))  # \u7ed3\u679c\u662f3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8d1f\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u8d1f\u6570\u7684\u5904\u7406\uff0c\u624b\u52a8\u5b9e\u73b0\u7684\u7b97\u6cd5\u4e0e<code>round()<\/code>\u51fd\u6570\u7565\u6709\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_round(number, decimals=0):<\/p>\n<p>    factor = 10  decimals<\/p>\n<p>    if number &lt; 0:<\/p>\n<p>        return int(number * factor - 0.5) \/ factor<\/p>\n<p>    return int(number * factor + 0.5) \/ factor<\/p>\n<p>print(custom_round(-3.14159, 2))  # \u7ed3\u679c\u662f-3.14<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u91d1\u878d\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u8ba1\u7b97\u4e2d\uff0c\u56db\u820d\u4e94\u5165\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u5229\u606f\u3001\u6298\u6263\u548c\u7a0e\u8d39\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u7ed3\u679c\u56db\u820d\u4e94\u5165\u5230\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">amount = 123.456<\/p>\n<p>rounded_amount = round(amount, 2)<\/p>\n<p>print(rounded_amount)  # \u7ed3\u679c\u662f123.46<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u56db\u820d\u4e94\u5165\u4e5f\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u503c\u56db\u820d\u4e94\u5165\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;value&#39;: [3.14159, 2.71828, 1.61803]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;rounded_value&#39;] = df[&#39;value&#39;].apply(lambda x: round(x, 2))<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u56db\u820d\u4e94\u5165\u540c\u6837\u4e0d\u53ef\u6216\u7f3a\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u7269\u7406\u5e38\u6570\u548c\u5b9e\u9a8c\u6570\u636e\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u7ed3\u679c\u56db\u820d\u4e94\u5165\u4ee5\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([3.14159, 2.71828, 1.61803])<\/p>\n<p>rounded_data = np.round(data, 2)<\/p>\n<p>print(rounded_data)  # \u7ed3\u679c\u662f[3.14, 2.72, 1.62]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong>\uff1a\u5728\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7cbe\u5ea6\u95ee\u9898\u3002\u4f8b\u5982\uff0c<code>round(2.675, 2)<\/code>\u7684\u7ed3\u679c\u662f2.67\u800c\u4e0d\u662f2.68\u3002\u8fd9\u662f\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u5f0f\u5bfc\u81f4\u7684\u3002<\/li>\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u7b49\u5e93\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u4e00\u81f4\u6027\u95ee\u9898<\/strong>\uff1a\u5728\u4e0d\u540c\u7684Python\u7248\u672c\u4e2d\uff0c<code>round()<\/code>\u51fd\u6570\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\u3002\u5728\u9700\u8981\u786e\u4fdd\u4e00\u81f4\u6027\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u624b\u52a8\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5b9e\u73b0\u56db\u820d\u4e94\u5165\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u3001<code>math<\/code>\u5e93\u4e2d\u7684<code>math.ceil()<\/code>\u548c<code>math.floor()<\/code>\u51fd\u6570\u3001\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b9e\u73b0\u7684\u65b9\u6cd5\u4ee5\u53ca\u624b\u52a8\u5b9e\u73b0\u7684\u7b97\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u3001\u66f4\u51c6\u786e\u5730\u5904\u7406\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u56db\u820d\u4e94\u5165\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>round()<\/code>\u51fd\u6570\u8fdb\u884c\u56db\u820d\u4e94\u5165\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u9700\u8981\u56db\u820d\u4e94\u5165\u7684\u6570\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5c0f\u6570\u70b9\u540e\u8981\u4fdd\u7559\u7684\u4f4d\u6570\u3002\u6bd4\u5982\uff0c<code>round(3.14159, 2)<\/code>\u5c06\u8fd4\u56de3.14\uff0c\u800c<code>round(2.675, 2)<\/code>\u5219\u4f1a\u8fd4\u56de2.67\uff0c\u8fd9\u8868\u660e\u6d6e\u70b9\u6570\u7684\u8868\u793a\u53ef\u80fd\u5bfc\u81f4\u4e00\u4e9b\u610f\u5916\u7ed3\u679c\u3002<\/p>\n<p><strong>Python\u4e2d\u4f7f\u7528\u56db\u820d\u4e94\u5165\u7684\u5e38\u89c1\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u56db\u820d\u4e94\u5165\u5e38\u7528\u4e8e\u8d22\u52a1\u8ba1\u7b97\u3001\u7edf\u8ba1\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u7b49\u9886\u57df\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u4ef7\u683c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u91d1\u989d\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u53c8\u5982\uff0c\u5728\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u8ba1\u7b97\u5e73\u5747\u503c\u540e\u53ef\u80fd\u9700\u8981\u56db\u820d\u4e94\u5165\u5230\u7279\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\uff0c\u4ee5\u4fbf\u66f4\u7b80\u6d01\u5730\u5448\u73b0\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u56db\u820d\u4e94\u5165\u7684\u89c4\u5219\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u66f4\u7075\u6d3b\u7684\u56db\u820d\u4e94\u5165\u89c4\u5219\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Decimal<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u521b\u5efa<code>Decimal<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u820d\u5165\u65b9\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528<code>ROUND_HALF_UP<\/code>\u3001<code>ROUND_HALF_DOWN<\/code>\u7b49\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from decimal import Decimal, ROUND_HALF_UP\n\nnumber = Decimal(&#39;2.675&#39;)\nrounded_number = number.quantize(Decimal(&#39;0.01&#39;), rounding=ROUND_HALF_UP)\nprint(rounded_number)  # \u8f93\u51fa 2.68\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u66f4\u51c6\u786e\u5730\u5904\u7406\u8d22\u52a1\u76f8\u5173\u7684\u56db\u820d\u4e94\u5165\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u56db\u820d\u4e94\u5165\u662f\u6211\u4eec\u5728\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\u5e38\u5e38\u9047\u5230\u7684\u95ee\u9898\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u56db\u820d\u4e94\u5165\uff0c\u5305\u62ec\u5185\u7f6e\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1125400,"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\/1125394"}],"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=1125394"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125394\/revisions"}],"predecessor-version":[{"id":1125403,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1125394\/revisions\/1125403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1125400"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1125394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1125394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1125394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}