今天在
Pycharm
编译器中使用
round()
函数的时候编译器提示了警告,后经修改去掉了警告,这里做一下记录。
代码如下:
import numpy as np
x = np.array([0, 1])
y = np.sum(np.sin(x))
print(f"x四捨六入五留双后的值为{round(y, 3)}")
编译器提示信息:
事实上,在有些情况下,编译器会直接报错,代码如下:
import numpy as np
x = np.array([1])
y = x + 0.2732
print(f"x四舍五入后的值为{round(y, 3)}")
此时会提示:
numpy
的
ndarray
类型没有
round()
函数。那么为了避免和警告信息的出现,我们可以使用一下两种方式来解决这个问题。
方法一:使用类型强制转换
import numpy as np
x = np.array([1])
y = x + 0.2732
print(f"x四捨六入五留双后的值为{round(float(x), 3)}")
"""
result:
x四捨六入五留双后的值为1.273
"""
方法二:使用
np.round()
函数
import numpy as np
x = np.array([1])
y = x + 0.2732
print(f"x四捨六入五留双后的值为{np.round(y, 3)}")
"""
result:
x四捨六入五留双后的值为[1.273]
"""