numpy around() 四舍五入函数

numpy.around() 函数返回指定数字的四舍五入值。

语法:

numpy.around(a,decimals)

参数说明:

  • a: 数组
  • decimals: 舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

实例

import numpy as np
 
a = np.array([1.0,5.55,  123,  0.567,  25.532])  
print  ('原数组:')
print (a)
print ('\n')
print ('舍入后:')
print (np.around(a))
print (np.around(a, decimals =  1))
print (np.around(a, decimals =  -1))
输出结果为:

原数组:
[  1.      5.55  123.      0.567  25.532]


舍入后:
[  1.   6. 123.   1.  26.]
[  1.    5.6 123.    0.6  25.5]
[  0.  10. 120.   0.  30.]