numpy 指定浮点数的小数位数

问题来源

        做GUI的时候发现数据太长了,显示出来不好看,所以有这个需求

       

numpy.around函数

原型

numpy.around(a, decimals=0, out=None)

参数

  • a:要转换的np数组
  • decimals:要保留的位数,默认为0
  • out :Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary. See doc.ufuncs (Section “Output arguments”) for details.(没用到不知道是啥,这英文也没看懂)

返回值

  • 和输入数组一样大小的np数组,里面的每个元素都保留了decimals位小数

示例

>>> np.around([1.23,4.56,7.89])
array([1.,4.,7])
>>> np.around([1.23,4.56,7.89], decimals=1)
array([1.2,4.6,7.9])
>>> np.around([1.23,4.56,7.89], decimals=2)
array([1.23,4.56,7.89])
posted on 2021-06-06 19:18  雾恋过往  阅读(667)  评论(0编辑  收藏  举报

Live2D