Home > Mobile >  9 decimal places of array elements in Python
9 decimal places of array elements in Python

Time:06-19

How do I ensure that all the array elements after an operation are accurate to 9 decimal places? The desired output is attached.

import numpy as np
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
A1=A*2
print([A1])

The desired output is

[array([[ 2.000000000,  4.000000000,  6.000000000],
       [ 8.000000000, 10.000000000, 12.000000000],
       [14.000000000, 16.000000000, 18.000000000]])]

CodePudding user response:

You can use np.set_printoptions.

import numpy as np

A = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=float)
A1 = A * 2

np.set_printoptions(precision=9, floatmode='fixed')
print(A1)

You need to use dtype when creating the numpy array, since you're passing in a list of integers. Using floatmode='fixed' tells numpy to always print precision number of decimal places.

  • Related