{"id":1137064,"date":"2025-01-08T21:43:51","date_gmt":"2025-01-08T13:43:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137064.html"},"modified":"2025-01-08T21:43:53","modified_gmt":"2025-01-08T13:43:53","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%b5%ae%e7%82%b9%e6%95%b0%e5%90%91%e4%b8%8a%e5%8f%96%e6%95%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1137064.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101011\/8e0e0bc7-7de1-4ab7-ad61-f417e94e04ac.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>math.ceil()<\/code>\u51fd\u6570\u548c\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\uff0c\u5982<code>int()<\/code>\u7ed3\u5408\u52a0\u6cd5\u64cd\u4f5c\u3002<\/strong> \u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001math.ceil()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>math.ceil()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u7528\u4e8e\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u7684\u65b9\u6cd5\u3002\u8be5\u51fd\u6570\u5c5e\u4e8e<code>math<\/code>\u6a21\u5757\uff0c\u9700\u8981\u5148\u5bfc\u5165\u8be5\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>float_num = 3.14<\/p>\n<p>ceil_num = math.ceil(float_num)<\/p>\n<p>print(ceil_num)  # \u8f93\u51fa\uff1a4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>math.ceil()<\/code>\u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u5927\u4e8e\u6216\u7b49\u4e8e\u8f93\u5165\u6d6e\u70b9\u6570\u7684\u6700\u5c0f\u6574\u6570\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u5904\u7406\u6570\u5b66\u8ba1\u7b97\u3001\u7edf\u8ba1\u5206\u6790\u3001\u79d1\u5b66\u8ba1\u7b97\u7b49\u9886\u57df\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u5546\u54c1\u4ef7\u683c\u65f6\u9700\u8981\u5411\u4e0a\u53d6\u6574\u4ee5\u907f\u514d\u4ef7\u683c\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001int()\u7ed3\u5408\u52a0\u6cd5\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>math.ceil()<\/code>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u6ca1\u6709\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u7684\u6743\u9650\u6216\u5e0c\u671b\u907f\u514d\u4f7f\u7528\u5916\u90e8\u6a21\u5757\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u7ed3\u5408\u52a0\u6cd5\u64cd\u4f5c\u6765\u5b9e\u73b0\u5411\u4e0a\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b<\/p>\n<p>float_num = 3.14<\/p>\n<p>ceil_num = int(float_num) + (float_num &gt; int(float_num))<\/p>\n<p>print(ceil_num)  # \u8f93\u51fa\uff1a4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5148\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u7136\u540e\u68c0\u67e5\u6d6e\u70b9\u6570\u662f\u5426\u5927\u4e8e\u8f6c\u6362\u540e\u7684\u6574\u6570\u3002\u5982\u679c\u662f\uff0c\u5219\u52a01\uff0c\u4ece\u800c\u5b9e\u73b0\u5411\u4e0a\u53d6\u6574\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u6027\u80fd\u6709\u8f83\u9ad8\u8981\u6c42\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u5bfc\u5165\u989d\u5916\u7684\u6a21\u5757\uff0c\u51cf\u5c11\u4e86\u8fd0\u884c\u65f6\u7684\u5f00\u9500\u3002\u4f46\u5176\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\uff0c\u4e0d\u63a8\u8350\u5728\u590d\u6742\u9879\u76ee\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001numpy.ceil()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u79d1\u5b66\u8ba1\u7b97\u6216\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>ceil()<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>float_num = 3.14<\/p>\n<p>ceil_num = np.ceil(float_num)<\/p>\n<p>print(ceil_num)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy.ceil()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6d6e\u70b9\u6570\uff0c\u4f46\u5176\u503c\u5df2\u7ecf\u662f\u5411\u4e0a\u53d6\u6574\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u9002\u5408\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3001\u79d1\u5b66\u8ba1\u7b97\u7b49\u9886\u57df\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5f53\u60a8\u5df2\u7ecf\u5728\u4f7f\u7528<code>numpy<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5229\u7528\u5176\u5185\u7f6e\u51fd\u6570\uff0c\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001pandas\u4e2d\u7684ceil\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>pandas<\/code>\u5e93\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u7528\u7684\u5de5\u5177\u3002\u867d\u7136<code>pandas<\/code>\u672c\u8eab\u6ca1\u6709\u76f4\u63a5\u7684<code>ceil<\/code>\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408<code>numpy.ceil()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>data = {&#39;values&#39;: [3.14, 2.71, 1.41]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;ceil_values&#39;] = np.ceil(df[&#39;values&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5f53\u60a8\u9700\u8981\u5bf9\u6574\u4e2a\u6570\u636e\u6846\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u6709\u7279\u5b9a\u9700\u6c42\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_ceil(x):<\/p>\n<p>    return int(x) + (x &gt; int(x))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>float_num = 3.14<\/p>\n<p>ceil_num = custom_ceil(float_num)<\/p>\n<p>print(ceil_num)  # \u8f93\u51fa\uff1a4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5728\u9700\u8981\u9ad8\u5ea6\u5b9a\u5236\u5316\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5f53\u60a8\u9700\u8981\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\u4e14\u8fd9\u4e9b\u73af\u5883\u7684\u4f9d\u8d56\u5e93\u4e0d\u540c\u6b65\u65f6\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong><code>math.ceil()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<strong><code>int()<\/code>\u7ed3\u5408\u52a0\u6cd5\u64cd\u4f5c<\/strong>\u867d\u7136\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\uff0c\u4f46\u5728\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u6548\u3002<strong><code>numpy.ceil()<\/code>\u51fd\u6570<\/strong>\u5219\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u79d1\u5b66\u8ba1\u7b97\u6216\u6570\u636e\u5206\u6790\u4efb\u52a1\u4e2d\u4f7f\u7528\u3002\u6700\u540e\uff0c\u5bf9\u4e8e\u9ad8\u5ea6\u5b9a\u5236\u5316\u7684\u9700\u6c42\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6d6e\u70b9\u6570\u7684\u5411\u4e0a\u53d6\u6574\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>ceil()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u6d6e\u70b9\u6570\u7684\u5411\u4e0a\u53d6\u6574\u3002\u8fd9\u4e2a\u51fd\u6570\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u5230\u6700\u8fd1\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0c<code>math.ceil(4.2)<\/code>\u4f1a\u8fd4\u56de5\uff0c\u800c<code>math.ceil(-4.2)<\/code>\u4f1a\u8fd4\u56de-4\u3002\u4f7f\u7528\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165<code>math<\/code>\u6a21\u5757\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u4e2d\u8fdb\u884c\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>math.ceil()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u7b97\u672f\u8fd0\u7b97\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>int(x) + (x % 1 &gt; 0)<\/code>\u7684\u65b9\u5f0f\u6765\u5224\u65ad\u6d6e\u70b9\u6570\u662f\u5426\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5982\u679c\u6709\uff0c\u5219\u52a01\u4ee5\u5b9e\u73b0\u5411\u4e0a\u53d6\u6574\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u9700\u8981\u5411\u4e0a\u53d6\u6574\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u5411\u4e0a\u53d6\u6574\u5e38\u5e38\u5728\u9700\u8981\u786e\u4fdd\u5206\u914d\u3001\u5e93\u5b58\u3001\u5bb9\u91cf\u7b49\u95ee\u9898\u65f6\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u67d0\u79cd\u6750\u6599\u6216\u8d44\u6e90\u7684\u9700\u6c42\u65f6\uff0c\u5982\u679c\u6bcf\u4e2a\u5355\u4f4d\u53ea\u80fd\u6574\u4ef6\u51fa\u552e\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5411\u4e0a\u53d6\u6574\u4ee5\u786e\u4fdd\u6709\u8db3\u591f\u7684\u8d44\u6e90\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u6d6e\u70b9\u6570\u5411\u4e0a\u53d6\u6574\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528math.ceil()\u51fd\u6570\u548c\u5176\u4ed6\u66ff\u4ee3\u65b9\u6cd5\uff0c\u5982int [&hellip;]","protected":false},"author":3,"featured_media":1137070,"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\/1137064"}],"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=1137064"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137064\/revisions"}],"predecessor-version":[{"id":1137071,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137064\/revisions\/1137071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1137070"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1137064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1137064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1137064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}