Skip to content

quantity.to_string(precision=<int>) does not produce a string of the quantity with <int> decimals of precision #11131

Closed

Description

@pennucci

Description

The precision argument to the to_string method of a Quantity object doesn't appear to do what it should.

Expected behavior

I expect quantity.to_string(precision=) to produce a string of the quantity with decimals of precision.

Actual behavior

quantity.to_string(precision=) produces a string with the full precision of quantity.

Steps to Reproduce

from astropy import units as u
q = 10.12345 * u.km
print("%s"%q.to_string(precision=1))  # the full precision is printed

System Details

Linux-5.4.0-53-generic-x86_64-with-glibc2.29
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0]
Numpy 1.17.4
astropy 4.1rc2
Scipy 1.3.3
Matplotlib 3.1.2

Activity

  1. pllim commented on Dec 4, 2020

    @pllim
    Member

    Seems like it only works if a Numpy array is given.

    >>> q = [10.12345] * u.m
    >>> print("%s"%q.to_string(precision=1))
    [10.1] m

    Behavior is the same as https://numpy.org/doc/stable/reference/generated/numpy.set_printoptions.html#numpy.set_printoptions . So even if this is a bug, maybe it is a upstream bug? @mhvk ?

  2. ludwigschwardt commented on Dec 7, 2020

    @ludwigschwardt
    Contributor

    I've noticed something similar but in the context of Angle... I've gone ahead and opened a separate issue in the end.

  3. jmmaljaars commented on Dec 7, 2020

    @jmmaljaars
    Contributor

    The precision remained unused in case the format variable evaluated to the default value. #11145 should fix this. Please note, however, that the "general format" g is used rather than the "floating point format" f! So once #11145 is merged, the snippet

    from astropy import units as u
    q = 10.12345 * u.km
    print("%s"%q.to_string(precision=4))
    

    returns

    10.12 km
    
  4. added a commit that references this issue on Jan 6, 2021
    179bf32
  5. added 2 commits that reference this issue on Feb 21, 2021
    d395e3f
    4c23287
  6. added a commit that references this issue on Feb 28, 2021
    45dd305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions