如何将输入变量舍入到某个数字

时间:2017-07-26 15:41:09

标签: python-3.5

我正在使用python 3+,我想要将变量舍入到500,如果输入高于500,那么它会向上舍入到1000.有没有办法我可以使用math.ceil()或round()? 到目前为止我已经这样做了,但我不确定我是否以正确的方式遇到过它。

import math
x = int(input("how much data did you use this month? "))
math.ceil(x / 500.0) * 500.0
print(x)

无论数字是多少,我想要将x舍入为500,但如果它更高(例如 - 600)我希望它将其舍入为1000.最后一行不起作用,只打印用户输入的内容。

2 个答案:

答案 0 :(得分:1)

你试过了吗?

x = math.ceil(x / 500.0) * 500.0

在打印x变量之前更新x变量?

答案 1 :(得分:1)

math.ceil()返回您期望的值,但您没有将它分配给任何东西。 只需将该值赋给变量即可。

这是解决方案:

 import math
 x = int(input("how much data did you use this month? "))
 x = math.ceil(x / 500.0) * 500.0

 print(x)
相关问题