擅长:python、mysql、java
<h2>四舍五入到最接近的X(即最接近的20.0)</h2>
<ol>
<li>只需除以要舍入的值</li>
<li>然后<code>round</code>结果</li>
<li>然后将它乘以要舍入到的数字并转换为整数</li>
</ol>
<p>例如</p>
<pre><code>round_to_nearest = 20
for a_num in [9,15,22,32,35,66,98]:
rounded = int(round(a_num/round_to_nearest)*round_to_nearest)
print("{a_num} rounded = ".format(a_num=a_num,r=rounded))
</code></pre>
<h2>转过来</h2>
<p>哦,没关系,看起来你只是想</p>
<pre><code>print(round(12.3),round(12.6)) # 12, 13
</code></pre>
<p>如果<code>round</code>舍入错误(即<a href="https://repl.it/repls/EffectiveDisastrousInitialization" rel="nofollow noreferrer">round(12.5) => 12 in python3</a>),您只需在数字上加0.5就可以了</p>
<pre><code>int(12.5+0.5)
</code></pre>