在数据处理的时候常常会用到 四舍五入,有时候需要精确到十分位,有时候需要精确到十位,这时候可以用round(number, decimal=’?’)来实现,decimal 就是控制小数点移动的位数,左-右+。具体如下:
import numpy as np
a = np.array([1.136, 2.317, 2.65964, 123.3356, 4.61475])
print('原始数据\n', a)
print('四舍五入,精确到个位\n', np.round(a))
print('四舍五入,精确到十分位\n', np.round(a, decimals=1))
print('四舍五入,精确到百分位\n', np.round(a, decimals=2))
print('四舍五入,精确到十位\n', np.round(a, decimals=-1))
运行结果:
原始数据
[ 1.136 2.317 2.65964 123.3356 4.61475]
四舍五入,精确到个位
[ 1. 2. 3. 123. 5.]
四舍五入,精确到十分位
[ 1.1 2.3 2.7 123.3 4.6]
四舍五入,精确到百分位
[ 1.14 2.32 2.66 123.34 4.61]
四舍五入,精确到十位
[ 0. 0. 0. 120. 0.]