{"id":1181284,"date":"2025-01-15T18:49:14","date_gmt":"2025-01-15T10:49:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1181284.html"},"modified":"2025-01-15T18:49:17","modified_gmt":"2025-01-15T10:49:17","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%88%90%e5%88%86%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1181284.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370\u6210\u5206\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25125701\/53d5d39a-7a43-478d-8233-617ae88eb00e.webp\" alt=\"python\u5982\u4f55\u6253\u5370\u6210\u5206\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u6253\u5370\u6210\u5206\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u3001<code>sympy<\/code>\u5e93\u3001<code>math<\/code>\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757<\/strong>\u3002<code>fractions.Fraction<\/code>\u7c7b\u53ef\u4ee5\u5c06\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u51fa\u6765\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u6765\u6253\u5370\u6210\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>fractions<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86<code>Fraction<\/code>\u7c7b\u6765\u8868\u793a\u6709\u7406\u6570\u3002\u901a\u8fc7\u8fd9\u4e2a\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e00\u4e2a\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u5f62\u5f0f\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u6709\u7406\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<code>fractions.Fraction<\/code>\u7c7b\u5c06\u4e00\u4e2a\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num1 = 0.75<\/p>\n<p>fraction1 = Fraction(num1)<\/p>\n<p>print(f&quot;{num1} as a fraction is {fraction1}&quot;)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num2 = &#39;1.25&#39;<\/p>\n<p>fraction2 = Fraction(num2)<\/p>\n<p>print(f&quot;{num2} as a fraction is {fraction2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>Fraction<\/code>\u7c7b\uff0c\u7136\u540e\u5c06\u4e00\u4e2a\u6d6e\u70b9\u6570\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>0.75 as a fraction is 3\/4<\/p>\n<p>1.25 as a fraction is 5\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6570\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p><code>Fraction<\/code>\u7c7b\u4e0d\u4ec5\u53ef\u4ee5\u5c06\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u5206\u6570\u8fd0\u7b97\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fractions import Fraction<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u5206\u6570<\/strong><\/h2>\n<p>fraction1 = Fraction(3, 4)<\/p>\n<p>fraction2 = Fraction(5, 6)<\/p>\n<h2><strong>\u5206\u6570\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = fraction1 + fraction2<\/p>\n<p>print(f&quot;{fraction1} + {fraction2} = {result_add}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u51cf\u6cd5<\/strong><\/h2>\n<p>result_sub = fraction1 - fraction2<\/p>\n<p>print(f&quot;{fraction1} - {fraction2} = {result_sub}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = fraction1 * fraction2<\/p>\n<p>print(f&quot;{fraction1} * {fraction2} = {result_mul}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u9664\u6cd5<\/strong><\/h2>\n<p>result_div = fraction1 \/ fraction2<\/p>\n<p>print(f&quot;{fraction1} \/ {fraction2} = {result_div}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5206\u6570<code>fraction1<\/code>\u548c<code>fraction2<\/code>\uff0c\u5e76\u8fdb\u884c\u4e86\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>3\/4 + 5\/6 = 19\/12<\/p>\n<p>3\/4 - 5\/6 = -1\/12<\/p>\n<p>3\/4 * 5\/6 = 15\/24<\/p>\n<p>3\/4 \/ 5\/6 = 9\/10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u5230\uff0c<code>Fraction<\/code>\u7c7b\u4f7f\u5f97\u5206\u6570\u8fd0\u7b97\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>sympy<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>sympy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u5e93\uff0c\u5b83\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u7b26\u53f7\u6570\u5b66\u8fd0\u7b97\u3002\u4f7f\u7528<code>sympy<\/code>\u5e93\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>sympy<\/code>\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\u5b83\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>sympy<\/code>\u5e93\u5c06\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import Rational<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num1 = 0.75<\/p>\n<p>fraction1 = Rational(num1)<\/p>\n<p>print(f&quot;{num1} as a fraction is {fraction1}&quot;)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num2 = &#39;1.25&#39;<\/p>\n<p>fraction2 = Rational(num2)<\/p>\n<p>print(f&quot;{num2} as a fraction is {fraction2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>sympy<\/code>\u5e93\u4e2d\u7684<code>Rational<\/code>\u7c7b\uff0c\u5c06\u4e00\u4e2a\u6d6e\u70b9\u6570\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>0.75 as a fraction is 3\/4<\/p>\n<p>1.25 as a fraction is 5\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6570\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p><code>sympy<\/code>\u5e93\u4e2d\u7684<code>Rational<\/code>\u7c7b\u4e5f\u652f\u6301\u5404\u79cd\u5206\u6570\u8fd0\u7b97\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import Rational<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u5206\u6570<\/strong><\/h2>\n<p>fraction1 = Rational(3, 4)<\/p>\n<p>fraction2 = Rational(5, 6)<\/p>\n<h2><strong>\u5206\u6570\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = fraction1 + fraction2<\/p>\n<p>print(f&quot;{fraction1} + {fraction2} = {result_add}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u51cf\u6cd5<\/strong><\/h2>\n<p>result_sub = fraction1 - fraction2<\/p>\n<p>print(f&quot;{fraction1} - {fraction2} = {result_sub}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = fraction1 * fraction2<\/p>\n<p>print(f&quot;{fraction1} * {fraction2} = {result_mul}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u9664\u6cd5<\/strong><\/h2>\n<p>result_div = fraction1 \/ fraction2<\/p>\n<p>print(f&quot;{fraction1} \/ {fraction2} = {result_div}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5206\u6570<code>fraction1<\/code>\u548c<code>fraction2<\/code>\uff0c\u5e76\u8fdb\u884c\u4e86\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>3\/4 + 5\/6 = 19\/12<\/p>\n<p>3\/4 - 5\/6 = -1\/12<\/p>\n<p>3\/4 * 5\/6 = 15\/24<\/p>\n<p>3\/4 \/ 5\/6 = 9\/10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u5230\uff0c<code>sympy<\/code>\u5e93\u4e5f\u4f7f\u5f97\u5206\u6570\u8fd0\u7b97\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>math<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u6570\u5b66\u51fd\u6570\u3002\u867d\u7136<code>math<\/code>\u6a21\u5757\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u5c06\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u7684\u529f\u80fd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u7ed3\u5408<code>fractions<\/code>\u6a21\u5757\u5c06\u4e00\u4e2a\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>from fractions import Fraction<\/p>\n<h2><strong>\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num1 = 0.75<\/p>\n<p>fraction1 = Fraction(num1).limit_denominator()<\/p>\n<p>print(f&quot;{num1} as a fraction is {fraction1}&quot;)<\/p>\n<h2><strong>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5206\u6570<\/strong><\/h2>\n<p>num2 = &#39;1.25&#39;<\/p>\n<p>fraction2 = Fraction(num2).limit_denominator()<\/p>\n<p>print(f&quot;{num2} as a fraction is {fraction2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>math<\/code>\u6a21\u5757\u548c<code>fractions<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5c06\u4e00\u4e2a\u6d6e\u70b9\u6570\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>0.75 as a fraction is 3\/4<\/p>\n<p>1.25 as a fraction is 5\/4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6570\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u7ed3\u5408<code>fractions<\/code>\u6a21\u5757\u8fdb\u884c\u5206\u6570\u8fd0\u7b97\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>from fractions import Fraction<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u5206\u6570<\/strong><\/h2>\n<p>fraction1 = Fraction(3, 4).limit_denominator()<\/p>\n<p>fraction2 = Fraction(5, 6).limit_denominator()<\/p>\n<h2><strong>\u5206\u6570\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = fraction1 + fraction2<\/p>\n<p>print(f&quot;{fraction1} + {fraction2} = {result_add}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u51cf\u6cd5<\/strong><\/h2>\n<p>result_sub = fraction1 - fraction2<\/p>\n<p>print(f&quot;{fraction1} - {fraction2} = {result_sub}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = fraction1 * fraction2<\/p>\n<p>print(f&quot;{fraction1} * {fraction2} = {result_mul}&quot;)<\/p>\n<h2><strong>\u5206\u6570\u9664\u6cd5<\/strong><\/h2>\n<p>result_div = fraction1 \/ fraction2<\/p>\n<p>print(f&quot;{fraction1} \/ {fraction2} = {result_div}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5206\u6570<code>fraction1<\/code>\u548c<code>fraction2<\/code>\uff0c\u5e76\u8fdb\u884c\u4e86\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\u3002\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>3\/4 + 5\/6 = 19\/12<\/p>\n<p>3\/4 - 5\/6 = -1\/12<\/p>\n<p>3\/4 * 5\/6 = 15\/24<\/p>\n<p>3\/4 \/ 5\/6 = 9\/10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u5230\uff0c\u7ed3\u5408<code>math<\/code>\u6a21\u5757\u548c<code>fractions<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u5206\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5982\u4f55\u6253\u5370\u6210\u5206\u6570\u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>sympy<\/code>\u5e93\u4ee5\u53ca\u7ed3\u5408<code>math<\/code>\u6a21\u5757\u548c<code>fractions<\/code>\u6a21\u5757\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u4e00\u4e2a\u6570\u8f6c\u6362\u4e3a\u5206\u6570\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u5206\u6570\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\uff0c\u5e76\u4e14\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5904\u7406\u5206\u6570\u76f8\u5173\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6d6e\u70b9\u6570\u683c\u5f0f\u5316\u4e3a\u5206\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fractions<\/code>\u6a21\u5757\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u5206\u6570\u3002\u8be5\u6a21\u5757\u63d0\u4f9b\u4e86<code>Fraction<\/code>\u7c7b\uff0c\u53ef\u4ee5\u63a5\u6536\u6d6e\u70b9\u6570\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u5206\u6570\u8868\u793a\u3002\u4f8b\u5982\uff0c<code>from fractions import Fraction<\/code>\u540e\uff0c\u4f7f\u7528<code>Fraction(0.75).limit_denominator()<\/code>\u53ef\u4ee5\u5f97\u5230<code>3\/4<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4f60\u5f97\u5230\u6700\u7b80\u5206\u6570\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6253\u5370\u5206\u6570\u65f6\u6709\u54ea\u4e9b\u5e38\u7528\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u5206\u6570\u5bf9\u8c61\u3002\u901a\u8fc7<code>fractions.Fraction<\/code>\u7c7b\u521b\u5efa\u5206\u6570\u540e\uff0c\u4f7f\u7528<code>print(Fraction(1, 2))<\/code>\u4f1a\u8f93\u51fa<code>1\/2<\/code>\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5c55\u793a\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u8d1f\u6570\u5206\u6570\uff1f<\/strong><br \/>\u5904\u7406\u8d1f\u6570\u5206\u6570\u65f6\uff0c<code>fractions<\/code>\u6a21\u5757\u540c\u6837\u6709\u6548\u3002\u521b\u5efa\u5206\u6570\u65f6\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u8d1f\u53f7\uff0c\u4f8b\u5982<code>Fraction(-1, 2)<\/code>\u5c06\u8fd4\u56de<code>-1\/2<\/code>\u3002\u6253\u5370\u65f6\uff0c\u8d1f\u53f7\u4f1a\u81ea\u52a8\u663e\u793a\u3002\u5982\u679c\u5e0c\u671b\u5c06\u8d1f\u53f7\u653e\u5728\u5206\u6570\u7684\u5206\u5b50\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Fraction(1, -2)<\/code>\uff0c\u8fd9\u5c06\u8f93\u51fa<code>-1\/2<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u6253\u5370\u6210\u5206\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528fractions\u6a21\u5757\u3001sympy\u5e93\u3001math\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1181296,"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\/1181284"}],"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=1181284"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181284\/revisions"}],"predecessor-version":[{"id":1181298,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1181284\/revisions\/1181298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1181296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1181284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1181284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1181284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}