是否有一个内置函数可以像下面这样舍入?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
是否有一个内置函数可以像下面这样舍入?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
当前回答
round(x[, n]):数值四舍五入到10的负n次方的最接近倍数。所以如果n是负的…
def round5(x):
return int(round(x*2, -1)) / 2
由于10 = 5 * 2,您可以对2使用整数除法和乘法,而不是对5.0使用浮点除法和乘法。这并不重要,除非你喜欢位移位
def round5(x):
return int(round(x << 1, -1)) >> 1
其他回答
如果有人需要“财务四舍五入”(0.5位总是向上):
def myround(x, base=5):
roundcontext = decimal.Context(rounding=decimal.ROUND_HALF_UP)
decimal.setcontext(roundcontext)
return int(base *float(decimal.Decimal(x/base).quantize(decimal.Decimal('0'))))
根据文档,其他舍入选项是:
ROUND_CEILING(朝向无限) ROUND_DOWN(趋近于零) ROUND_FLOOR(朝向-∞) ROUND_HALF_DOWN(当平局趋于0时最接近) ROUND_HALF_EVEN(最接近偶数) ROUND_HALF_UP(到最接近的平局从0开始) ROUND_UP(远离零) ROUND_05UP(如果四舍五入后的最后一位为0或5,则远离0;否则趋于零)
默认情况下,Python使用ROUND_HALF_EVEN,因为它有一些统计优势(四舍五入的结果没有偏见)。
divround的修改版本:-)
def divround(value, step, barrage):
result, rest = divmod(value, step)
return result*step if rest < barrage else (result+1)*step
那么这个呢:
def divround(value, step):
return divmod(value, step)[0] * step
另一种方法(不需要显式的乘法或除法运算符):
def rnd(x, b=5):
return round(x + min(-(x % b), b - (x % b), key=abs))
Use:
>>> def round_to_nearest(n, m):
r = n % m
return n + m - r if r + r >= m else n - r
它不使用乘法,也不会从/转换为浮点数。
四舍五入到最接近10的倍数:
>>> for n in range(-21, 30, 3): print('{:3d} => {:3d}'.format(n, round_to_nearest(n, 10)))
-21 => -20
-18 => -20
-15 => -10
-12 => -10
-9 => -10
-6 => -10
-3 => 0
0 => 0
3 => 0
6 => 10
9 => 10
12 => 10
15 => 20
18 => 20
21 => 20
24 => 20
27 => 30
如你所见,它对负数和正数都适用。平局(例如-15和15)总是向上四舍五入。
一个类似的例子,四舍五入到5的最接近倍数,证明它也表现为不同的“基数”:
>>> for n in range(-21, 30, 3): print('{:3d} => {:3d}'.format(n, round_to_nearest(n, 5)))
-21 => -20
-18 => -20
-15 => -15
-12 => -10
-9 => -10
-6 => -5
-3 => -5
0 => 0
3 => 5
6 => 5
9 => 10
12 => 10
15 => 15
18 => 20
21 => 20
24 => 25
27 => 25