{"id":1028882,"date":"2024-12-31T11:03:27","date_gmt":"2024-12-31T03:03:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028882.html"},"modified":"2024-12-31T11:03:30","modified_gmt":"2024-12-31T03:03:30","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e5%a4%a7%e4%ba%8e%e4%b8%a4%e4%bd%8d%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1028882.html","title":{"rendered":"Python\u5982\u4f55\u5224\u65ad\u5927\u4e8e\u4e24\u4f4d\u5c0f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ff524630-63a3-4b3c-ad40-91cd6f01c6ba.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u5224\u65ad\u5927\u4e8e\u4e24\u4f4d\u5c0f\u6570\" \/><\/p>\n<p><p> <strong>Python\u5224\u65ad\u5927\u4e8e\u4e24\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5229\u7528\u5c0f\u6570\u7cbe\u5ea6\u7684\u6bd4\u8f83\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u662f\u4e00\u79cd\u6bd4\u8f83\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u68c0\u67e5\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u7cbe\u786e\u5730\u5339\u914d\u6570\u503c\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u68c0\u67e5\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u662f\u5426\u8d85\u8fc7\u4e24\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_more_than_two_decimal_places(num):<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    if &#39;.&#39; in num_str:<\/p>\n<p>        decimal_part = num_str.split(&#39;.&#39;)[1]<\/p>\n<p>        return len(decimal_part) &gt; 2<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u7528\u5728\u5feb\u901f\u9a8c\u8bc1\u573a\u666f\u3002\u7136\u800c\uff0c\u5982\u679c\u8f93\u5165\u7684\u6d6e\u70b9\u6570\u662f\u901a\u8fc7\u8ba1\u7b97\u83b7\u5f97\u7684\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u56e0\u4e3a\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u800c\u4e0d\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5c0f\u6570\u7cbe\u5ea6\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684Decimal\u6a21\u5757\u6765\u8fdb\u884c\u7cbe\u5ea6\u7684\u4e25\u683c\u63a7\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from decimal import Decimal, getcontext<\/p>\n<p>def is_more_than_two_decimal_places(num):<\/p>\n<p>    getcontext().prec = 28  # \u8bbe\u7f6e\u7cbe\u5ea6<\/p>\n<p>    decimal_num = Decimal(num)<\/p>\n<p>    return decimal_num.as_tuple().exponent &lt; -2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u907f\u514d\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cDecimal\u5bf9\u8c61\u7684\u8ba1\u7b97\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u539f\u751f\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5c0f\u6570\u70b9\u540e\u8d85\u8fc7\u4e24\u4f4d\u7684\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def is_more_than_two_decimal_places(num):<\/p>\n<p>    return bool(re.match(r&#39;^\\d+\\.\\d{3,}$&#39;, str(num)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u591f\u7cbe\u786e\u5339\u914d\u6570\u503c\u683c\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u65b9\u4fbf\u5730\u6269\u5c55\u5230\u5176\u4ed6\u683c\u5f0f\u7684\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u5176\u4ed6\u6761\u4ef6\u7684\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u6761\u4ef6\u6765\u5224\u65ad\u6570\u503c\u7684\u6709\u6548\u6027\u3002\u4f8b\u5982\uff0c\u5728\u91d1\u878d\u6570\u636e\u5904\u7406\u4e2d\uff0c\u901a\u5e38\u4e0d\u4ec5\u9700\u8981\u5224\u65ad\u5c0f\u6570\u4f4d\u6570\uff0c\u8fd8\u8981\u786e\u4fdd\u6570\u503c\u5728\u5408\u7406\u7684\u8303\u56f4\u5185\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_number(num):<\/p>\n<p>    if num &lt; 0 or num &gt; 1000000:  # \u5047\u8bbe\u6709\u6548\u6570\u503c\u8303\u56f4\u4e3a0\u52301000000<\/p>\n<p>        return False<\/p>\n<p>    return is_more_than_two_decimal_places(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u6d6e\u70b9\u6570\u5728\u8ba1\u7b97\u673a\u4e2d\u6709\u65f6\u4f1a\u4ea7\u751f\u7cbe\u5ea6\u95ee\u9898\uff0c\u5bfc\u81f4\u7ed3\u679c\u4e0d\u5982\u9884\u671f\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6765\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_more_than_two_decimal_places(num):<\/p>\n<p>    num_str = f&quot;{num:.20f}&quot;  # \u4fdd\u7559\u8db3\u591f\u591a\u7684\u7cbe\u5ea6<\/p>\n<p>    if &#39;.&#39; in num_str:<\/p>\n<p>        decimal_part = num_str.split(&#39;.&#39;)[1].rstrip(&#39;0&#39;)<\/p>\n<p>        return len(decimal_part) &gt; 2<\/p>\n<p>    return False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u8d22\u52a1\u8ba1\u7b97<\/strong>\uff1a\u5728\u8d22\u52a1\u8ba1\u7b97\u4e2d\uff0c\u7cbe\u786e\u7684\u5c0f\u6570\u4f4d\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u5229\u606f\u6216\u6c47\u7387\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4fdd\u7559\u591a\u4f4d\u5c0f\u6570\u3002<\/li>\n<li><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong>\uff1a\u5728\u79d1\u5b66\u7814\u7a76\u4e2d\uff0c\u6570\u636e\u7684\u7cbe\u786e\u5ea6\u5f80\u5f80\u9700\u8981\u63a7\u5236\u5728\u975e\u5e38\u7ec6\u5fae\u7684\u5c42\u9762\u3002\u4f8b\u5982\uff0c\u5728\u5316\u5b66\u8ba1\u91cf\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7cbe\u786e\u5230\u5343\u5206\u4e4b\u4e00\u751a\u81f3\u66f4\u9ad8\u3002<\/li>\n<li><strong>\u7528\u6237\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a\u5728\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u9a8c\u8bc1\u4e2d\uff0c\u786e\u4fdd\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u4e0d\u8d85\u8fc7\u89c4\u5b9a\u7684\u8303\u56f4\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u6709\u6548\u6027\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f18\u5316\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\uff0c\u53ef\u4ee5\u5c06\u5e38\u7528\u7684\u5224\u65ad\u903b\u8f91\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u4fbf\u4e8e\u590d\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def has_more_than_two_decimal_places(num):<\/p>\n<p>    return is_more_than_two_decimal_places(num)<\/p>\n<p>def is_valid_and_precise(num):<\/p>\n<p>    return is_valid_number(num) and has_more_than_two_decimal_places(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u6765\u786e\u4fdd\u51fd\u6570\u7684\u6b63\u786e\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def test_is_more_than_two_decimal_places():<\/p>\n<p>    assert is_more_than_two_decimal_places(123.456) == True<\/p>\n<p>    assert is_more_than_two_decimal_places(123.45) == False<\/p>\n<p>    assert is_more_than_two_decimal_places(123) == False<\/p>\n<p>    assert is_more_than_two_decimal_places(0.001) == True<\/p>\n<p>    print(&quot;All tests passed!&quot;)<\/p>\n<p>test_is_more_than_two_decimal_places()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5224\u65ad\u6570\u503c\u662f\u5426\u8d85\u8fc7\u4e24\u4f4d\u5c0f\u6570\uff0c\u5e76\u5e94\u7528\u5230\u4e0d\u540c\u7684\u5b9e\u9645\u573a\u666f\u4e2d\u3002\u786e\u4fdd\u4ee3\u7801\u7684\u7cbe\u786e\u6027\u548c\u9c81\u68d2\u6027\uff0c\u662f\u5f00\u53d1\u9ad8\u8d28\u91cf\u8f6f\u4ef6\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u6709\u8d85\u8fc7\u4e24\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u68c0\u67e5\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u6765\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str()<\/code>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u4f7f\u7528<code>split(&#39;.&#39;)<\/code>\u65b9\u6cd5\u5c06\u5176\u5206\u5272\uff0c\u6700\u540e\u68c0\u67e5\u5c0f\u6570\u90e8\u5206\u7684\u957f\u5ea6\u3002\u5982\u679c\u5c0f\u6570\u90e8\u5206\u7684\u957f\u5ea6\u5927\u4e8e2\uff0c\u5219\u8868\u793a\u8be5\u6570\u5b57\u6709\u8d85\u8fc7\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u6709\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u5c0f\u6570\u4f4d\u6570\u7684\u5224\u65ad\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u76f4\u63a5\u5224\u65ad\u5c0f\u6570\u4f4d\u6570\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>decimal<\/code>\u6a21\u5757\u4e2d\u7684<code>Decimal<\/code>\u7c7b\u6765\u8fdb\u884c\u66f4\u52a0\u7cbe\u786e\u7684\u8ba1\u7b97\u3002\u4f7f\u7528<code>Decimal<\/code>\u53ef\u4ee5\u907f\u514d\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\uff0c\u4ece\u800c\u66f4\u53ef\u9760\u5730\u5224\u65ad\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u4ee5\u786e\u4fdd\u5176\u5c0f\u6570\u4f4d\u6570\u7b26\u5408\u8981\u6c42\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u6355\u83b7\u8f93\u5165\u9519\u8bef\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u5c0f\u4e8e\u6216\u7b49\u4e8e\u4e24\u4f4d\u5c0f\u6570\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u8be5\u6570\u5b57\uff1b\u5982\u679c\u5927\u4e8e\u4e24\u4f4d\u5c0f\u6570\uff0c\u5219\u63d0\u793a\u7528\u6237\u91cd\u65b0\u8f93\u5165\uff0c\u5e76\u5faa\u73af\u76f4\u5230\u83b7\u5f97\u7b26\u5408\u6761\u4ef6\u7684\u8f93\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u7b26\u5408\u4f60\u7684\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5224\u65ad\u5927\u4e8e\u4e24\u4f4d\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u5229\u7528\u5c0f\u6570\u7cbe\u5ea6\u7684\u6bd4\u8f83\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1028895,"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\/1028882"}],"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=1028882"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028882\/revisions"}],"predecessor-version":[{"id":1028896,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028882\/revisions\/1028896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028895"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}