是否有一个内置函数可以像下面这样舍入?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
是否有一个内置函数可以像下面这样舍入?
10 -> 10
12 -> 10
13 -> 15
14 -> 15
16 -> 15
18 -> 20
当前回答
下一个5的倍数
考虑51需要转换为55:
code here
mark = 51;
r = 100 - mark;
a = r%5;
new_mark = mark + a;
其他回答
舍入到非整数值,例如0.05:
def myround(x, prec=2, base=.05):
return round(base * round(float(x)/base),prec)
我发现这很有用,因为我只需要在代码中进行搜索和替换,就可以将“round(”更改为“myround(”,而不必更改参数值。
divround的修改版本:-)
def divround(value, step, barrage):
result, rest = divmod(value, step)
return result*step if rest < barrage else (result+1)*step
我需要四舍五入到前面的5。
举个例子,16发到15发或者19发到15发
下面是使用的代码
def myround(x,segment):
preRound = x / segment
roundNum = int(preRound)
segVal = segment * roundNum
return segVal
def round_to_next5(n):
return n + (5 - n) % 5
那么这个呢:
def divround(value, step):
return divmod(value, step)[0] * step