题解 | #取近似值#
取近似值
https://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
方法1:通过ceil()向下取整,floor()向上取整。以及modf()函数获取输入数字的小数部分,然后判断 小数部分大于等于0.5向上取整,相反向下取整
import math
num = float(input())
if math.modf(num)[0] >= 0.5:
print(math.ceil(num))
else:
print(math.floor(num))
方法2:输入的数字+0.5再转换为整数即可
print(math.ceil(num))
else:
print(math.floor(num))
方法2:输入的数字+0.5再转换为整数即可
def func():
num = float(input())
res = num + 0.5
f = int(res)
print(f)
if __name__ == "__main__":
func()
num = float(input())
res = num + 0.5
f = int(res)
print(f)
if __name__ == "__main__":
func()