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.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to create an array using scientific notation and adjust the print precision using np.set_printoptions with suppress=False.
  • Create a function that converts an array of numbers into scientific notation strings with a specified precision.
  • Implement a solution that displays both the raw scientific notation and a formatted version with controlled decimal places.
  • Test the notation conversion on an array with very small numbers to verify that the output uses scientific notation with the desired precision.

Go to:


PREV : Create a random 10x4 array with precision control.
NEXT : Remove specific columns from a 2D array.


Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.