是否有内置函数可以像下面这样循环?
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.
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",而不必更改参数值.
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 中测试
用:
>>> 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)
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)
抱歉,我想评论Alok Singhai的回答,但由于缺乏声誉而不能让我= /
无论如何,我们可以再推广一个步骤:
def myround(x, base=5):
return base * round(float(x) / base)
Run Code Online (Sandbox Code Playgroud)
这使我们可以使用非整数基,例如.25
或任何其他小数基。
归档时间: |
|
查看次数: |
95330 次 |
最近记录: |