Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:49:33+00:00 2026-05-14T23:49:33+00:00

How do I print formatted NumPy arrays in a way similar to this: x

  • 0

How do I print formatted NumPy arrays in a way similar to this:

x = 1.23456
print('%.3f' % x)

If I want to print the numpy.ndarray of floats, it prints several decimals, often in ‘scientific’ format, which is rather hard to read even for low-dimensional arrays. However, numpy.ndarray apparently has to be printed as a string, i.e., with %s. Is there a solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:49:34+00:00Added an answer on May 14, 2026 at 11:49 pm

    Use numpy.set_printoptions to set the precision of the output:

    import numpy as np
    x = np.random.random(10)
    print(x)
    # [ 0.07837821  0.48002108  0.41274116  0.82993414  0.77610352  0.1023732
    #   0.51303098  0.4617183   0.33487207  0.71162095]
    
    np.set_printoptions(precision=3)
    print(x)
    # [ 0.078  0.48   0.413  0.83   0.776  0.102  0.513  0.462  0.335  0.712]
    

    And suppress suppresses the use of scientific notation for small numbers:

    y = np.array([1.5e-10, 1.5, 1500])
    print(y)
    # [  1.500e-10   1.500e+00   1.500e+03]
    
    np.set_printoptions(suppress=True)
    print(y)
    # [    0.      1.5  1500. ]
    

    To apply print options locally, using NumPy 1.15.0 or later, you could use the numpy.printoptions context manager.
    For example, inside the with-suite precision=3 and suppress=True are set:

    x = np.random.random(10)
    with np.printoptions(precision=3, suppress=True):
        print(x)
        # [ 0.073  0.461  0.689  0.754  0.624  0.901  0.049  0.582  0.557  0.348]
    

    But outside the with-suite the print options are back to default settings:

    print(x)    
    # [ 0.07334334  0.46132615  0.68935231  0.75379645  0.62424021  0.90115836
    #   0.04879837  0.58207504  0.55694118  0.34768638]
    

    If you are using an earlier version of NumPy, you can create the context manager
    yourself. For example,

    import numpy as np
    import contextlib
    
    @contextlib.contextmanager
    def printoptions(*args, **kwargs):
        original = np.get_printoptions()
        np.set_printoptions(*args, **kwargs)
        try:
            yield
        finally: 
            np.set_printoptions(**original)
    
    x = np.random.random(10)
    with printoptions(precision=3, suppress=True):
        print(x)
        # [ 0.073  0.461  0.689  0.754  0.624  0.901  0.049  0.582  0.557  0.348]
    

    To prevent zeros from being stripped from the end of floats:

    np.set_printoptions now has a formatter parameter which allows you to specify a format function for each type.

    np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
    print(x)
    

    which prints

    [ 0.078  0.480  0.413  0.830  0.776  0.102  0.513  0.462  0.335  0.712]
    

    instead of

    [ 0.078  0.48   0.413  0.83   0.776  0.102  0.513  0.462  0.335  0.712]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to show nicely formatted output regarding bandwidth speed during download I have this
I want to print html to a document but I want it formatted as
Any ideas on how this can be formatted to print out extra attributes in
What is the easiest way to pretty print (a.k.a. formatted) a org.w3c.dom.Document to stdout?
I want to call a function in formatted output of string using print( )
I want to print xml in pdf using itext in java, as well formatted
I am trying to print formatted xml to a file but my XmlNodePrinter just
I have an 8 digit integer which I would like to print formatted like
I have an array of strings that are formatted like this: $strings = array(
I have a text file that is formatted like JSON, but in a print/view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.