Skip to content

Commit d434857

Browse files
committed
Apply small updates to unit formatting docs
1 parent 64fe8fa commit d434857

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

‎docs/units/format.rst‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ String Representations of Units
66
Converting Units to String Representations
77
==========================================
88

9-
You can control the way that |Quantity| and |Unit| objects are rendered as
10-
strings using the `Python Format String Syntax
9+
You can control the way that |Quantity| and :class:`~astropy.units.Unit`
10+
objects are rendered as strings using the `Python Format String Syntax
1111
<https://docs.python.org/3/library/string.html#format-string-syntax>`_
1212
(demonstrated below using `f-strings
1313
<https://www.python.org/dev/peps/pep-0498/>`_).
1414

15-
For quantities, format specifiers, like ``0.003f`` will be applied to
15+
For quantities, format specifiers, like ``.3f`` will be applied to
1616
the |Quantity| value, without affecting the unit. Specifiers like
17-
``20s``, which would only apply to a string, will be applied to the
17+
``^20s``, which would only apply to a string, will be applied to the
1818
whole string representation of the |Quantity|.
1919

2020
Examples
2121
--------
2222

2323
.. EXAMPLE START: Converting Units to String Representations
2424
25-
To render |Quantity| or |Unit| objects as strings::
25+
To render |Quantity| or :class:`~astropy.units.Unit` objects as strings::
2626

2727
>>> from astropy import units as u
2828
>>> import numpy as np
@@ -31,23 +31,23 @@ To render |Quantity| or |Unit| objects as strings::
3131
<Quantity 10.5 km>
3232
>>> f"{q}"
3333
'10.5 km'
34-
>>> f"{q:+0.03f}"
34+
>>> f"{q:+.3f}"
3535
'+10.500 km'
36-
>>> f"{q:20s}"
37-
'10.5 km '
36+
>>> f"{q:^20s}"
37+
' 10.5 km '
3838

3939
To format both the value and the unit separately, you can access the |Quantity|
40-
class attributes within format strings::
40+
attributes within format strings::
4141

4242
>>> q = 10.5 * u.km
4343
>>> q
4444
<Quantity 10.5 km>
45-
>>> f"{q.value:0.003f} in {q.unit:s}"
45+
>>> f"{q.value:.3f} in {q.unit}"
4646
'10.500 in km'
4747

48-
Because ``numpy`` arrays do not accept most format specifiers, using specifiers
49-
like ``0.003f`` will not work when applied to a ``numpy`` array or non-scalar
50-
|Quantity|. Use :func:`numpy.array_str` instead. For instance::
48+
Because |ndarray| does not accept most format specifiers, using specifiers like
49+
``.3f`` will not work when applied to a |ndarray| or non-scalar |Quantity|. Use
50+
:func:`numpy.array_str` instead. For instance::
5151

5252
>>> q = np.linspace(0,1,10) * u.m
5353
>>> f"{np.array_str(q.value, precision=1)} {q.unit}" # doctest: +FLOAT_CMP
@@ -72,23 +72,23 @@ parameter to select a different format, including ``"latex"``, ``"unicode"``,
7272
'10.000 in $\\mathrm{km}$'
7373
>>> fluxunit = u.erg / (u.cm ** 2 * u.s)
7474
>>> f"{fluxunit}"
75-
u'erg / (cm2 s)'
75+
'erg / (cm2 s)'
7676
>>> print(f"{fluxunit:console}")
7777
erg
7878
------
7979
s cm^2
8080
>>> f"{fluxunit:latex}"
81-
u'$\\mathrm{\\frac{erg}{s\\,cm^{2}}}$'
81+
'$\\mathrm{\\frac{erg}{s\\,cm^{2}}}$'
8282
>>> f"{fluxunit:>20s}"
83-
u' erg / (cm2 s)'
83+
' erg / (cm2 s)'
8484

8585
The :meth:`~astropy.units.core.UnitBase.to_string` method is an alternative way
8686
to format units as strings, and is the underlying implementation of the
8787
`format`-style usage::
8888

8989
>>> fluxunit = u.erg / (u.cm ** 2 * u.s)
9090
>>> fluxunit.to_string('latex')
91-
u'$\\mathrm{\\frac{erg}{s\\,cm^{2}}}$'
91+
'$\\mathrm{\\frac{erg}{s\\,cm^{2}}}$'
9292

9393
Creating Units from Strings
9494
===========================
@@ -110,8 +110,8 @@ formats using the `~astropy.units.Unit` class::
110110
Creating units from strings requires the use of a specialized
111111
parser for the unit language, which results in a performance
112112
penalty if units are created using strings. Thus, it is much
113-
faster to use unit objects directly (e.g., ``unit = u.degree /
114-
u.minute``) instead of via string parsing (``unit =
113+
faster to use :class:`~astropy.units.Unit` objects directly (e.g.,
114+
``unit = u.degree / u.minute``) instead of via string parsing (``unit =
115115
u.Unit('deg/min')``). This parser is very useful, however, if your
116116
unit definitions are coming from a file format such as FITS or
117117
VOTable.
@@ -151,7 +151,7 @@ following formats:
151151
`IAU Style Manual
152152
<https://www.iau.org/static/publications/stylemanual1989.pdf>`_
153153
recommendations for unit presentation. This format is
154-
automatically used when printing a unit in the IPython notebook::
154+
automatically used when printing a unit in the `IPython`_ notebook::
155155

156156
>>> fluxunit # doctest: +SKIP
157157

0 commit comments

Comments
 (0)