Python3 - 这样的四舍五入组合

Python3 - Round up combination like this

我有一个浮点数组合(min_number、max_number),我期待以下汇总:

(-19.12, 34.45) "rounded to" (-19.2, 34.5)
(-0.34, -0.22) "rounded to" (-0.4, -0.3)
(-0.58, -0.87) "rounded to" (-0.6, -0.9)
(-0.24, 5.98) "rounded to" (-0.3, 6.0)
(2.57, 22.38) "rounded to" (2.6, 22.4)

请注意,只需要一位小数。然后:

size = int(input('please input: '))
number = (min_number + max_number) / size

那么“number”也需要四舍五入,还要求小数点后一位:

10.233333 "rounded to" 10.3
5.744542267888 "rounded to" 5.8

有什么想法吗?非常感谢,

只要乘以10,四舍五入再除以10

import math
num = input("Round a number:")
num = num.split()
print( math.ceil( float(num[0]) * 10 ) / 10,  math.ceil( float(num[1]) * 10 ) / 10 )

注意: 正如@0 0 指出的那样,如果您想舍入负数,这对负数不起作用,那么您需要为它们添加一个 if 语句并使用math.floor