【发布时间】:2016-03-04 06:13:57
【问题描述】:
因为“round”会将诸如24 down之类的数字四舍五入到20,所以当我需要将答案up四舍五入到30时。请帮帮我!我已经被困了很久了:(
【问题讨论】:
-
请告诉我们您实际制作的功能...会更容易为您提供帮助
-
使用
x + (-x) % 10。
因为“round”会将诸如24 down之类的数字四舍五入到20,所以当我需要将答案up四舍五入到30时。请帮帮我!我已经被困了很久了:(
【问题讨论】:
x + (-x) % 10。
导入数学
定义综述(x):
#rounding method
return int(math.ceil(x / 10.0)) * 10
【讨论】:
此函数将正确地向上和向下舍入:
import math
def roundup(x, n=10):
res = math.ceil(x/n)*n
if (x%n < n/2)and (x%n>0):
res-=n
return res
num = [5,9,11,15]
r_num = [roundup(n) for n in num]
print(r_num)
# [10, 10, 10, 20]
【讨论】: