{"id":1038912,"date":"2024-12-31T12:28:10","date_gmt":"2024-12-31T04:28:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1038912.html"},"modified":"2024-12-31T12:28:13","modified_gmt":"2024-12-31T04:28:13","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%b0%8f%e6%95%b0%e5%8f%98%e6%88%90%e5%88%86%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1038912.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5c0f\u6570\u53d8\u6210\u5206\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e1a85e14-bc65-4cff-a906-b09025888f6e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u5c0f\u6570\u53d8\u6210\u5206\u6570\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5c06\u5c0f\u6570\u53d8\u6210\u5206\u6570<\/strong>  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002<strong>\u4f7f\u7528<code>Fraction<\/code>\u7c7b\u3001\u8bbe\u7f6e\u5206\u6570\u7684\u7cbe\u5ea6\u3001\u5904\u7406\u7279\u6b8a\u5c0f\u6570\u60c5\u51b5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>Fraction<\/code>\u7c7b<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>fractions<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Fraction<\/code>\u7684\u7c7b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002<code>Fraction<\/code>\u7c7b\u53ef\u4ee5\u63a5\u53d7\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u4ee5\u53ca\u5176\u4ed6\u6570\u503c\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>decimal_number = 0.75<\/p>\n<p>fraction = Fraction(decimal_number)<\/p>\n<p>print(fraction)  # \u8f93\u51fa: 3\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u5206\u6570\u7684\u7cbe\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u63a7\u5236\u5206\u6570\u7684\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c\u5f53\u5c0f\u6570\u6709\u5f88\u591a\u4f4d\u65f6\uff0c\u76f4\u63a5\u8f6c\u6362\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u5206\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u9650\u5236\u5206\u6bcd\u7684\u6700\u5927\u503c\u6765\u63a7\u5236\u5206\u6570\u7684\u7cbe\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u9650\u5236\u5206\u6bcd\u7684\u6700\u5927\u503c\u4e3a100<\/strong><\/h2>\n<p>decimal_number = 0.333333<\/p>\n<p>fraction = Fraction(decimal_number).limit_denominator(100)<\/p>\n<p>print(fraction)  # \u8f93\u51fa: 1\/3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u7279\u6b8a\u5c0f\u6570\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u67d0\u4e9b\u7279\u6b8a\u7684\u5c0f\u6570\uff0c\u5982\u65e0\u9650\u5faa\u73af\u5c0f\u6570\uff0c\u53ef\u80fd\u9700\u8981\u7279\u522b\u5904\u7406\u3002<code>Fraction<\/code>\u7c7b\u53ef\u4ee5\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u4f46\u60a8\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684\u6570\u503c\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u65e0\u9650\u5faa\u73af\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>decimal_number = 1\/3<\/p>\n<p>fraction = Fraction(decimal_number)<\/p>\n<p>print(fraction)  # \u8f93\u51fa: 1\/3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5c0f\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5c0f\u6570\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u7ed9\u51fa\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>Fraction<\/code>\u7c7b\u8fdb\u884c\u8f6c\u6362\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6d6e\u70b9\u6570\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>decimal_string = &quot;0.75&quot;<\/p>\n<p>fraction = Fraction(decimal_string)<\/p>\n<p>print(fraction)  # \u8f93\u51fa: 3\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06\u5206\u6570\u8f6c\u6362\u56de\u5c0f\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u53ef\u80fd\u9700\u8981\u5c06\u5206\u6570\u8f6c\u6362\u56de\u5c0f\u6570\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u9664\u6cd5\u8fd0\u7b97\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u5206\u6570\u8f6c\u6362\u56de\u5c0f\u6570<\/strong><\/h2>\n<p>fraction = Fraction(3, 4)<\/p>\n<p>decimal_number = float(fraction)<\/p>\n<p>print(decimal_number)  # \u8f93\u51fa: 0.75<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u8d1f\u6570\u548c\u96f6<\/h3>\n<\/p>\n<p><p><code>Fraction<\/code>\u7c7b\u540c\u6837\u53ef\u4ee5\u5904\u7406\u8d1f\u6570\u548c\u96f6\u3002\u65e0\u8bba\u8f93\u5165\u662f\u6b63\u6570\u3001\u8d1f\u6570\u8fd8\u662f\u96f6\uff0c\u90fd\u53ef\u4ee5\u6b63\u786e\u8f6c\u6362\u4e3a\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5904\u7406\u8d1f\u6570<\/strong><\/h2>\n<p>negative_decimal = -0.5<\/p>\n<p>negative_fraction = Fraction(negative_decimal)<\/p>\n<p>print(negative_fraction)  # \u8f93\u51fa: -1\/2<\/p>\n<h2><strong>\u5904\u7406\u96f6<\/strong><\/h2>\n<p>zero_decimal = 0.0<\/p>\n<p>zero_fraction = Fraction(zero_decimal)<\/p>\n<p>print(zero_fraction)  # \u8f93\u51fa: 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u5408NumPy\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5c06NumPy\u6570\u7ec4\u4e2d\u7684\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528NumPy\u548c<code>fractions<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from fractions import Fraction<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5c0f\u6570\u7684NumPy\u6570\u7ec4<\/strong><\/h2>\n<p>decimal_array = np.array([0.5, 0.75, 0.333333])<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u4e2d\u7684\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>fraction_array = np.array([Fraction(d).limit_denominator(100) for d in decimal_array])<\/p>\n<p>print(fraction_array)  # \u8f93\u51fa: [1\/2 3\/4 1\/3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u5b66\u6559\u80b2<\/strong>\uff1a\u5e2e\u52a9\u5b66\u751f\u7406\u89e3\u5206\u6570\u4e0e\u5c0f\u6570\u7684\u5173\u7cfb\u3002<\/li>\n<li><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong>\uff1a\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4f7f\u7528\u5206\u6570\u8868\u793a\u66f4\u4e3a\u7cbe\u786e\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u67d0\u4e9b\u7edf\u8ba1\u6570\u636e\u53ef\u80fd\u9700\u8981\u4ee5\u5206\u6570\u5f62\u5f0f\u5448\u73b0\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002<strong>\u4f7f\u7528<code>Fraction<\/code>\u7c7b\u3001\u8bbe\u7f6e\u5206\u6570\u7684\u7cbe\u5ea6\u3001\u5904\u7406\u7279\u6b8a\u5c0f\u6570\u60c5\u51b5\u3001\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5c0f\u6570\u8f6c\u6362\u3001\u5c06\u5206\u6570\u8f6c\u6362\u56de\u5c0f\u6570\u3001\u5904\u7406\u8d1f\u6570\u548c\u96f6\u3001\u7ed3\u5408NumPy\u4f7f\u7528<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u6db5\u76d6\u4e86\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u5c06\u4f7f\u60a8\u7684Python\u7f16\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u7cbe\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u6765\u8f7b\u677e\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>Fraction<\/code>\u7c7b\uff0c\u53ef\u4ee5\u63a5\u53d7\u6d6e\u70b9\u6570\u5e76\u81ea\u52a8\u5c06\u5176\u8f6c\u6362\u4e3a\u6700\u7b80\u5206\u6570\u3002\u4f8b\u5982\uff0c<code>from fractions import Fraction<\/code>\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Fraction(0.75)<\/code>\u6765\u5f97\u5230<code>3\/4<\/code>\u3002<\/p>\n<p><strong>\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u7ed3\u679c\u662f\u6700\u7b80\u5f62\u5f0f\uff1f<\/strong><br \/>\u4f7f\u7528<code>fractions.Fraction<\/code>\u7c7b\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u7b80\u5316\u5206\u6570\u3002\u65e0\u8bba\u60a8\u8f93\u5165\u7684\u662f\u4ec0\u4e48\u5c0f\u6570\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u90fd\u5c06\u662f\u6700\u7b80\u5f62\u5f0f\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>Fraction(0.25)<\/code>\u5c06\u8fd4\u56de<code>1\/4<\/code>\uff0c\u800c\u4e0d\u662f<code>2\/8<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01<code>Fraction<\/code>\u7c7b\u540c\u6837\u652f\u6301\u5b57\u7b26\u4e32\u8f93\u5165\u3002\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5c0f\u6570\uff0c\u4f8b\u5982<code>Fraction(&#39;0.8&#39;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>4\/5<\/code>\u3002\u8fd9\u5728\u5904\u7406\u6765\u81ea\u6587\u672c\u6587\u4ef6\u6216\u7528\u6237\u8f93\u5165\u7684\u5c0f\u6570\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5c06\u5c0f\u6570\u53d8\u6210\u5206\u6570 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528fractions\u6a21\u5757\u5c06\u5c0f\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002\u4f7f\u7528Frac [&hellip;]","protected":false},"author":3,"featured_media":1038922,"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\/1038912"}],"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=1038912"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038912\/revisions"}],"predecessor-version":[{"id":1038923,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1038912\/revisions\/1038923"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1038922"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1038912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1038912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1038912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}