Terrence Brannon
Terrence Brannon

Reputation: 4968

How to set the number of digits after the decimal place displayed in a numpy float?

Per the docs I set the number of digits of precision for printing a numpy float to 8 and expected to see 1.12345679 from this code but did not:

import numpy as np

np.set_printoptions(precision=8)

x = np.float_(1.123456789)

print x

Upvotes: 1

Views: 14026

Answers (1)

axwr
axwr

Reputation: 2236

As the comments have suggested you could use numpy.around.

import numpy as np

np.set_printoptions(precision=4)

x = np.float_(1.123456789)
print x

x = np.around(x, 8)
print x

This outputs:

1.123456789
1.12345679

Upvotes: 3

Related Questions