Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string formatting information to unit tutorial #3119

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions tutorials/unit_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@
# operation can be performed:
print(3 * units.inch + 5 * units.cm)

#########################################################################
# ``pint`` by default will print full unit names for :class:`~pint.Quantity`.
print(f'{20 * units.meter ** 2}')

#########################################################################
# This can be reduced to symbolic by specifying a compact (~) formatter:
print(f'{20 * units.meter ** 2:~}')

#########################################################################
# A compact (~), pretty (P) formatter:
print(f'{20 * units.meter ** 2:~P}')

#########################################################################
# Place formatters following other print specifications:
print(f'{20 * units.meter ** 2:0.3f~P}')

#########################################################################
# Other string formatting options are available, see the `Pint string formatting specification
# <https://pint.readthedocs.io/en/stable/user/formatting.html>`_.

#########################################################################
# Converting Units
# ----------------
Expand Down