标签: python algorithm math rounding negative-number
我想使用python将负数四舍五入到最接近的高0.5。
例如:
-1.2 => -1 -0.8 => -0.5
等
如果我使用
round(number * 2) / 2.0
它向我返回最接近的低0.5(例如:-2.4向我返回-2.5而我想要-2.0)
如何在python中做到这一点?
答案 0 :(得分:6)
您可以使用ceil的{{1}}方法来获取所需的内容。如果使用正数,请使用math。
ceil
math
floor