Open In App

Print a NumPy Array Without Scientific Notation in Python

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We have given you a Numpy array. We are going to display the Numpy array without scientific notation and with the given precision. In this article, we are going to explore different methods in Python.

Example

Input 1:  Array = [123.456, 0.123456, 987.123] , Precision = 2
Output 1: [123.46 0.12 987.12]
Input 2: Array = [123.456, 0.123456, 987.123] , Precision = 3
Output 2: [123.456 0.123 987.123]

Print a NumPy Array Without Scientific Notation in Python

Below are the methods that are used to Print a NumPy Array Without Scientific Notation:

  • Using set_printoptions() with suppress, and precision as parameters.
  • Using set_printoptions() with .foramt()
  • Using array_str()and

Using set_printoptions() with suppress, and precision as parameters

Generally, Numpy array with complex float values results in an array with elements with scientific notations. To avoid this we can use set_printoptions with suppress, and precision as parameters. This will help us to achieve our desired array with no scientific notation and with precision. Now let’s get into code implementation.

Python3




import numpy as np
 
#defining the display funcrion
def GFG(arr,prec):
    np.set_printoptions(suppress=True,precision=prec)
    print(arr)
     
 
if __name__ == "__main__":
    #defining the numpy array with float values
    arr = np.array([123.456, 0.123456, 987.123])
     
    #displaying the original array
    print("Original Numpy Array : ")
    print(arr,"\n")
     
    #Converting the array to desired format and then displaying it
    print("Numpy Array without without scientific notation : ")
    GFG(arr,3)


Example:

Original Numpy Array : 
[1.23456e+02 1.23456e-01 9.87123e+02]
Numpy Array without scientific notation:
[123.456 0.123 987.123]

Using Using set_printoptions() with .foramt()

We can use .foramt() with the set_printoptions() to achieve the desired numpy array. Lets now move to the code implementation.

Python3




import numpy as np
 
#defining the display funcrion
def GFG(arr):
    np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
    print(arr)
 
if __name__ == "__main__":
    #defining the numpy array with float values
    arr = np.array([123.456789, 0.400123456789, 987.123456789])
     
    #displaying the original array
    print("Original Numpy Array : ")
    print(arr,"\n")
     
    #Converting the array to desired format and then displaying it
    print("Numpy Array without scientific notation : ")
    GFG(arr)
    


Output

Original Numpy Array : 
[1.23456789e+02 4.00123457e-01 9.87123457e+02]
Numpy Array without scientific notation :
[ 123.457 0.400 987.123]

Using array_str()

np.array_str() is a function in the Numpy library that is used to describe the array elements as strings. This method gives us the flexibility to set precision, suppress scientific notation, and format strings, thus helping us to generate the array without scientific notation and with the given precision. Let’s hop into the code implementation.

Python3




import numpy as np
 
#defining the display funcrion
def GFG(arr,prec):
    new = np.array_str(arr, precision=prec, suppress_small=True)
    print(new)
 
if __name__ == "__main__":
    #defining the numpy array with float values
    arr = np.array([123.456789, 0.400123456789, 987.123456789])
     
    #displaying the original array
    print("Original Numpy Array : ")
    print(arr,"\n")
     
    #Converting the array to desired format and then displaying it
    print("Numpy Array without scientific notation : ")
    GFG(arr,3)
    


Output

Original Numpy Array : 
[1.23456789e+02 4.00123457e-01 9.87123457e+02]
Numpy Array without scientific notation :
[123.457 0.4 987.123]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads