擅长:python、mysql、java
<p>我对这个过程花了多长时间感到厌烦(350次迭代循环是个杀手),但答案似乎很简单。一个<code>Decimal</code>对象不能按您的方式订阅。在</p>
<p>您可能需要先将其转换为<em>字符串</em>,然后再进行处理以获得数字:</p>
<pre><code>print str(pi)[:int(n)+1] # ignore decimal point in digit count.
</code></pre>
<p>您还应该记住,这个<em>会截断</em>值,而不是四舍五入。例如,当PI开始为:</p>
^{pr2}$
<p>(大概是我脑子里能记住的),将字符串截断为5个有效数字将得到<code>3.1415</code>,而不是更正确的<code>3.1416</code>。在</p>