<p>知道任何0的幂次都等于1是有帮助的。随着ndigits的增加,函数:</p>
<p>当您增加ndigit时,<code>f(ndigits) = 10<sup>-ndigits</sup></code>会变小。特别是当您将ndigits增加1时,只需将精度为1的小数位向左移动。e、 g.<code>10^-0 = 1</code>,<code>10^-1 = .1</code>和{<cd4>}。答案中1的位置是<code>round</code>的最后一个精度点。在</p>
<p>上面写着</p>
<blockquote>
<p>For the built-in types supporting round(), values are rounded to the
closest multiple of 10 to the power minus ndigits; if two multiples
are equally close, rounding is done toward the even choice (so, for
example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2).</p>
</blockquote>
<p>这在python3中有意外的行为,它将对所有float有效。考虑您给出的示例,<code>round(123.455, 2)</code>生成值123.45。这是预期的行为,因为<code>10^-2</code>的最接近的偶数倍是<code>123.46</code>,而不是{<cd9>}!在</p>
<p>要理解这一点,您必须特别注意以下注释:</p>
<blockquote>
<p>Note The behavior of round() for floats can be surprising: for
example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This
is not a bug: it’s a result of the fact that most decimal fractions
can’t be represented exactly as a float.</p>
</blockquote>
<p>这就是为什么某些浮动会舍入到“错误的值”,而且据我所知,确实没有简单的解决办法。(sadface)如果您希望获得不同于浮点不可预测行为的行为,可以使用分数(即表示分子和分母的两个变量)来表示自定义取整函数中的浮点值。在</p>