NumPy
Python
array formatting
scientific notation
precision handling

Pretty-print a NumPy array without scientific notation and with given precision

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

NumPy prints arrays using formatting rules that favor compactness, which is why you often see scientific notation such as 1.23e-05 or 4.56e+08. That is useful for raw numerical work, but it is not always ideal for logs, reports, demos, or debugging.

To pretty-print a NumPy array without scientific notation and with a fixed precision, you usually want either a local formatting context or a one-off string conversion. The best choice depends on whether the formatting should affect just one print call or the whole session.

The Simplest Local Solution

For most code, the cleanest answer is np.printoptions() as a context manager:

python
1import numpy as np
2
3arr = np.array([
4    [1234.56789, 0.000012345],
5    [98765.4321, 3.14159265],
6])
7
8with np.printoptions(precision=4, suppress=True):
9    print(arr)

Output looks like this:

text
[[ 1234.5679     0.    ]
 [98765.4321     3.1416]]

Here is what the options do:

  • 'precision=4 limits the printed decimal places'
  • 'suppress=True discourages scientific notation for small floating-point values'

Because this uses a context manager, the formatting applies only inside the with block.

Global Session-Wide Print Options

If you want the formatting to affect later prints too, use np.set_printoptions():

python
1import numpy as np
2
3np.set_printoptions(precision=3, suppress=True)
4
5arr = np.array([123456.789, 0.000456789, 42.424242])
6print(arr)

This is convenient in a notebook, but be careful: it changes global NumPy printing behavior for the rest of the process.

That is why np.printoptions() is often better in reusable application code.

array2string for One-Off Formatting

If you need a formatted string rather than printing directly, use np.array2string():

python
1import numpy as np
2
3arr = np.array([1234.56789, 0.000012345, 9.87654321])
4
5formatted = np.array2string(
6    arr,
7    precision=2,
8    suppress_small=True,
9)
10
11print(formatted)

This is useful when you are writing to logs, files, or UI components and want precise control over the resulting string.

Forcing a Custom Float Format

Sometimes you want a stricter format than NumPy's general print options provide. In that case, pass a custom formatter:

python
1import numpy as np
2
3arr = np.array([1234.56789, 0.000012345, 9.87654321])
4
5formatted = np.array2string(
6    arr,
7    formatter={"float_kind": lambda x: f"{x:.4f}"}
8)
9
10print(formatted)

Output:

text
[1234.5679 0.0000 9.8765]

This is the most explicit approach. It is especially handy when you want fixed-width decimal formatting regardless of NumPy's default heuristics.

Choose the Right Tool

A good rule of thumb is:

  • use np.printoptions() for a temporary local override
  • use np.set_printoptions() for notebook-wide or session-wide formatting
  • use np.array2string() when you need the result as a string
  • use a custom formatter when exact float formatting matters

The formatting choice does not change the underlying numeric values. It only changes how they are displayed.

Common Pitfalls

  • Using np.set_printoptions() in library code and unexpectedly changing global formatting for other parts of the program.
  • Expecting display formatting to round the actual stored values. It only affects output.
  • Forgetting that suppress=True is about notation style, not about the number of decimal places.
  • Printing very large arrays without adjusting threshold, then assuming values disappeared. NumPy may summarize long arrays with ellipses.
  • Confusing suppress_small=True in array2string() with the global suppress=True option. They solve related but slightly different formatting cases.

Summary

  • Use np.printoptions(precision=..., suppress=True) for the safest local pretty-printing.
  • Use np.set_printoptions() only when global behavior is acceptable.
  • Use np.array2string() when you need a formatted string rather than direct output.
  • Custom formatters give you the strictest control over decimal rendering.
  • Formatting changes the display, not the underlying array values.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.