擅长:python、mysql、java
<p>Python包含<code>round()</code>函数,该函数<a href="http://docs.python.org/library/functions.html#round">lets you specify</a>您需要的位数。从文档中:</p>
<blockquote>
<p><code>round(x[, n])</code></p>
<p>Return the floating point value x rounded to n digits after the decimal point. If n is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done away from 0 (so. for example, round(0.5) is 1.0 and round(-0.5) is -1.0).</p>
</blockquote>
<p>所以您需要使用<code>round(x, 2)</code>来进行正常舍入。为了确保数字总是向上舍入<em>您需要使用<code>ceil(x)</code>函数。同样,要将<em>向下取整</em>使用<code>floor(x)</code>。</p>