在Python中舍入到5(或其他数字)

Pyd*_* UA 147 python rounding

是否有内置函数可以像下面这样循环?

10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
Run Code Online (Sandbox Code Playgroud)

Alo*_*hal 266

我不知道Python中的标准函数,但这对我有用:

def myround(x, base=5):
    return int(base * round(float(x)/base))
Run Code Online (Sandbox Code Playgroud)

很容易理解上述原因.你想确保你的数字除以5是一个整数,正确舍入.所以,我们首先完成那个(round(float(x)/5)),然后因为我们除以5,我们也乘以5.最后的转换float是因为int在Python中返回一个浮点值.

我通过给它一个round()参数使函数更通用,默认为5.

  • 这是我偏执狂,但我更喜欢使用`floor()`和`ceil()`而不是铸造:`base*floor(x/base)` (6认同)
  • 如果只有整数和向下舍入,那么你也可以只做`x // base*base` (3认同)
  • @user666412 `math.floor` 和 `math.ceil` 不允许与自定义基础一起使用,因此首选项无关紧要。 (3认同)

CCK*_*CKx 43

对于舍入到非整数值,例如0.05:

def myround(x, prec=2, base=.05):
  return round(base * round(float(x)/base),prec)
Run Code Online (Sandbox Code Playgroud)

我发现这很有用,因为我可以在我的代码中进行搜索和替换以更改"round("到"myround",而不必更改参数值.

  • 您可以使用:`def my_round(x,prec = 2,base = 0.05):return(base *(np.array(x)/ base).round())。round(prec)也接受numpy数组。 (2认同)

amo*_*ej1 20

这只是一个扩展的问题

>>> a=[10,11,12,13,14,15,16,17,18,19,20]
>>> for b in a:
...     int(round(b/5.0)*5.0)
... 
10
10
10
15
15
15
15
15
20
20
20
Run Code Online (Sandbox Code Playgroud)


小智 12

删除'休息'将起作用:

rounded = int(val) - int(val) % 5
Run Code Online (Sandbox Code Playgroud)

如果值是canady整数:

rounded = val - val % 5
Run Code Online (Sandbox Code Playgroud)

作为一个功能:

def roundint(value, base=5):
    return int(value) - int(value) % int(base)
Run Code Online (Sandbox Code Playgroud)


Kir*_*kow 11

def round_up_to_base(x, base=10):
    return x + (base - x) % base

def round_down_to_base(x, base=10):
    return x - (x % base)
Run Code Online (Sandbox Code Playgroud)

这使

为了base=5

>>> [i for i in range(20)]
[0, 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Run Code Online (Sandbox Code Playgroud)
>>> [round_down_to_base(x=i, base=5) for i in range(20)]
[0, 0,  0,  0,  0,  5,  5,  5,  5,  5,  10, 10, 10, 10, 10, 15, 15, 15, 15, 15]

>>> [round_up_to_base(x=i, base=5) for i in range(20)]
[0, 5,  5,  5,  5,  5,  10, 10, 10, 10, 10, 15, 15, 15, 15, 15, 20, 20, 20, 20]
Run Code Online (Sandbox Code Playgroud)

为了base=10

>>> [i for i in range(20)]
[0, 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Run Code Online (Sandbox Code Playgroud)
>>> [round_down_to_base(x=i, base=10) for i in range(20)]
[0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  10, 10, 10, 10, 10, 10, 10, 10, 10, 10]

>>> [round_up_to_base(x=i, base=10) for i in range(20)]
[0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20]
Run Code Online (Sandbox Code Playgroud)

在 Python 3.7.9 中测试


And*_*ong 8

def round_to_next5(n):
    return n + (5 - n) % 5
Run Code Online (Sandbox Code Playgroud)


wou*_*lee 7

用:

>>> def round_to_nearest(n, m):
        r = n % m
        return n + m - r if r + r >= m else n - r
Run Code Online (Sandbox Code Playgroud)

它不使用乘法,也不会从/转换为浮点数。

四舍五入到最接近的 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
Run Code Online (Sandbox Code Playgroud)

如您所见,它适用于负数和正数。关系(例如 -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
Run Code Online (Sandbox Code Playgroud)


pwd*_*son 6

round(x [,n]):将值舍入为函数减去n的最接近的10的倍数.所以如果n是负面的......

def round5(x):
    return int(round(x*2, -1)) / 2
Run Code Online (Sandbox Code Playgroud)

由于10 = 5*2,你可以使用整数除法和乘法2,而不是浮点除法和乘以5.0.这并不重要,除非你喜欢有点转移

def round5(x):
    return int(round(x << 1, -1)) >> 1
Run Code Online (Sandbox Code Playgroud)


Art*_*urJ 5

抱歉,我想评论Alok Singhai的回答,但由于缺乏声誉而不能让我= /

无论如何,我们可以再推广一个步骤:

def myround(x, base=5):
    return base * round(float(x) / base)
Run Code Online (Sandbox Code Playgroud)

这使我们可以使用非整数基,例如.25或任何其他小数基。

  • 不过,这本身就是一个答案。我使用了它,但没有将其定义为函数:y = base * round(float(x) / base)。只要您已经定义了 x 和 base,它就可以工作。请注意,这个答案获得了七票赞成。 (3认同)