<pre><code>int(round(x))
</code></pre>
<p>将其舍入并将其更改为整数</p>
<p><strong>编辑:</strong></p>
<p>您没有将int(round(h))赋值给任何变量。当您调用int(round(h))时,它返回整数,但不执行其他操作;您必须将该行更改为:</p>
<pre><code>h = int(round(h))
</code></pre>
<p>将新值赋给h</p>
<p><strong>编辑2:</strong></p>
<p>正如@prowleman在评论中所说,Python的<code>round()</code>并不像人们通常期望的那样工作,这是因为数字作为变量存储的方式通常不是您在屏幕上看到的方式。有很多答案可以解释这种行为:</p>
<p><a href="https://stackoverflow.com/questions/56820/round-in-python-doesnt-seem-to-be-rounding-properly#56833">round() in Python doesn't seem to be rounding properly</a></p>
<p>避免这个问题的一种方法是使用这个答案中的小数:<a href="https://stackoverflow.com/a/15398691/4345659">https://stackoverflow.com/a/15398691/4345659</a></p>
<p>为了使这个答案在不使用额外库的情况下正常工作,可以方便地使用自定义舍入函数。经过大量修改,我提出了以下解决方案,只要我测试,就避免了所有的存储问题。它基于使用字符串表示,通过<code>repr()</code>(而不是<code>str()</code>!)获得。它看起来很粗糙,但这是我找到的解决所有案件的唯一方法。它同时适用于Python2和Python3。</p>
<pre><code>def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
return float(num[:-1])
</code></pre>
<p>测试:</p>
<pre><code>>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>>
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0
</code></pre>
<p>最后,正确的答案是:</p>
<pre><code># Having proper_round defined as previously stated
h = int(proper_round(h))
</code></pre>
<p><strong>编辑3:</strong></p>
<p>测试:</p>
<pre><code>>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1 # should be 7
</code></pre>
<p>这里的问题是,第<code>dec</code>个十进制数可以是9,如果第<code>dec+1</code>个数字>;=5,9将变成0,1应该被带到第<code>dec-1</code>个数字。</p>
<p>如果我们考虑到这一点,我们会得到:</p>
<pre class="lang-py prettyprint-override"><code>def proper_round(num, dec=0):
num = str(num)[:str(num).index('.')+dec+2]
if num[-1]>='5':
a = num[:-2-(not dec)] # integer part
b = int(num[-2-(not dec)])+1 # decimal part
return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
return float(num[:-1])
</code></pre>
<p>在上面描述的情况下,<code>b = 10</code>和上一个版本将只连接<code>a</code>和<code>b</code>,这将导致<code>10</code>的连接,其中尾随的0将消失。此版本将<code>b</code>转换为基于<code>dec</code>的右小数位,作为适当的进位。</p>