如果一个是25使用python如何得到30

时间:2011-03-29 03:19:36

标签: python math

这是我的代码:

from math import ceil
a = 25
a = float(a/10)
a = int(ceil(a))*10
print a

我得到20,但我想得到30,

接下来是我想要的:

if the a is 22 , i want get 20
if the a is 25 , i want get 30
if the a is 27 , i want get 30
if the a is 21 , i want get 20

所以我该怎么做,

感谢

2 个答案:

答案 0 :(得分:7)

您正在寻找round() function

print int(round(25, -1))

答案 1 :(得分:2)

您可以使用round() method

>>> num = 25
>>> round_num = int(round(num, -1))
>>> round_num
30

>>> num = 22
>>> round_num = int(round(num, -1))
>>> round_num
20

等等。