w3resource

NumPy: Suppresses the use of scientific notation for small numbers in NumPy array


Suppress Scientific Notation in Array

Write a NumPy program to suppress the use of scientific notation for small numbers in a NumPy array.

Sample Solution:

Python Code:

# Importing the NumPy library and aliasing it as 'np'
import numpy as np

# Creating a NumPy array 'x' containing floating-point values
x = np.array([1.6e-10, 1.6, 1200, .235])

# Printing a message indicating the original array elements will be shown
print("Original array elements:")

# Printing the original array 'x' with its elements
print(x)

# Printing a message indicating the suppression of scientific notation in the array display
print("Array - scientific notation is suppressed")

# Setting the print options to suppress scientific notation in array printing
np.set_printoptions(suppress=True)

# Printing the array 'x' with scientific notation suppressed
print(x)

Sample Output:

Original array elements:
[1.60e-10 1.60e+00 1.20e+03 2.35e-01]
Print array values with precision 3:
[   0.       1.6   1200.       0.235]

Explanation:

In the above code –

x = np.array([1.6e-10, 1.6, 1200, .235]): Create a NumPy array x containing 4 float values, including a very small number and numbers with different magnitudes.

np.set_printoptions(suppress=True): Set the print options for NumPy arrays, specifying that the scientific notation should be suppressed when displaying the elements.

print(x): Print the NumPy array x.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to display small numbers without scientific notation using np.set_printoptions.
  • Create a function that formats an array to fixed-point notation and verifies that scientific notation is suppressed.
  • Test an array with very small and very large numbers to ensure that the output uses standard decimal notation.
  • Experiment with np.set_printoptions parameters to adjust precision and suppress scientific format for various arrays.

Go to:


PREV : Display Float Array with Precision
NEXT : Generate 10 Integers from Generator


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.