w3resource

NumPy: Set the precision value to 6 and print the array of scientific notation numbers


Represent values in scientific notation with precision.

Write a NumPy program to create an array using scientific notation numbers. Set the precision value to 6 and print the array.

Sample Solution:

Python Code:

# Importing NumPy library
import numpy as np

# Creating an array with scientific notation values
nums = np.array([1.2e-7, 1.5e-6, 1.7e-5])

# Displaying the original array
print("Original arrays:")
print(nums)

# Setting the precision value to 10 and suppressing the scientific notation
np.set_printoptions(suppress=True, precision=10)

# Displaying the array with updated precision and suppressed scientific notation
print("Set the precision value to 10:")
print(nums) 

Sample Output:

Original arrays:
[1.2e-07 1.5e-06 1.7e-05]
Set the precision value to 10:
[0.00000012 0.0000015  0.000017  ]

Explanation:

In the above exercise -

nums = np.array([1.2e-7, 1.5e-6, 1.7e-5]) creates a NumPy array with three small floating-point numbers in scientific notation.

np.set_printoptions(suppress=True, precision=10) sets the global print options for NumPy. The suppress=True option disables the use of scientific notation when printing NumPy arrays, and the precision=10 option sets the number of decimal places displayed to 10.

print(nums): Finally print() function prints the generated array.

Python-Numpy Code Editor: