python_beginner
python_beginner

Reputation: 133

Formatting numpy float values

what is the easiest way to format a numpy float64 value like this:

8.928571429999999509e+02

to:

892.857

Upvotes: 2

Views: 1138

Answers (3)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

The natural way should be numpy.set_printoptions.

Example

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

Upvotes: 3

Ajeet Shah
Ajeet Shah

Reputation: 19813

You can define a custom function:

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

Upvotes: 2

Garima Singh
Garima Singh

Reputation: 223

You can use string formatters "%.3f" % 8.928571429999999509e+02

Upvotes: 2

Related Questions