如何在python 3.4.0中将数字四舍五入到最接近的10?

时间:2015-11-30 14:58:34

标签: numbers rounding

因为" round"将24 down 这样的数字舍入到二十,当我需要将答案舍入向上到30.请帮助我!我已经坚持了很久:(

2 个答案:

答案 0 :(得分:0)

导入数学

def roundup(x):

#rounding method
return int(math.ceil(x / 10.0)) * 10

答案 1 :(得分:0)

此功能将正确向上和向下取整:

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]