格式化numpy浮点值

时间:2016-06-06 07:13:06

标签: python numpy number-formatting

格式化这样的numpy float64值的最简单方法是什么:

8.928571429999999509e+02

为:

892.857

3 个答案:

答案 0 :(得分:3)

自然方式应为numpy.set_printoptions

示例

>>> np.set_printoptions(precision=3)
>>> print np.array([8.928571429999999509e+02])
[ 892.857]

答案 1 :(得分:2)

您可以使用字符串格式化程序 “%。3f”%8.928571429999999509e + 02

答案 2 :(得分:1)

您可以定义自定义功能:

>>> myformat = lambda x: "%.3f" % x
>>> myformat(8.928571429999999509e+02)
'892.857'