Skip to content

Commit

Permalink
pass # modifier to f & g formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jan 9, 2025
1 parent dd72367 commit 5e48f7f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion clean
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env sh

set nonomatch
rm -rf generated_settings
rm -rf generated_settings .tox
rm -rf .cache

# the rest is common to all python directories
Expand Down
16 changes: 12 additions & 4 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ provides many of the most common conversions for you:
93.206 Mmiles
Specifying Quantities
.....................
Creating Quantities
...................

Normally, creating a :class:`Quantity` takes one or two arguments. The first is
taken to be the value, and the second, if given, is taken to be the model, which
Expand Down Expand Up @@ -683,14 +683,22 @@ If you are interested in the units of a quantity, you can use:
>>> h_line.units
'Hz'
Or you can access both the value and the units, either as a tuple or in
a string:
Or you can access both the value and the units as a tuple:

.. code-block:: python
>>> h_line.as_tuple()
(1420405751.786, 'Hz')
Rendering Quantities
....................

Rendering a quantity convert it to a string. There are many ways to do so.
Using *str* renders using default settings:

.. code-block:: python
>>> str(h_line)
'1.4204 GHz'
Expand Down
8 changes: 4 additions & 4 deletions quantiphy/quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2587,11 +2587,11 @@ def format(self, template=''):
# code {{{3
match = FORMAT_SPEC.match(template)
if match:
align, alt_form, width, comma, prec, ftype, units = match.groups()
align, use_alt_form, width, comma, prec, ftype, units = match.groups()
scale = units if units else None
prec = int(prec) if prec else None
ftype = ftype if ftype else ''
alt_form = dict(strip_zeros=False, strip_radix=False) if alt_form else {}
alt_form = dict(strip_zeros=False, strip_radix=False) if use_alt_form else {}
if ftype and ftype in 'dnu':
if ftype == 'u':
value = scale if scale else self.units
Expand Down Expand Up @@ -2644,12 +2644,12 @@ def format(self, template=''):
))
else:
value = float(self)
value = '{0:{1}.{2}{3}}'.format(value, comma, prec, ftype)
value = '{0:{4}{1}.{2}{3}}'.format(value, comma, prec, ftype, use_alt_form)
value = self._map_leading_sign(value)
value = self._map_sign(value)
width = width.lstrip('0')
# format function treats 0 as a padding rather than a width
if self.strip_zeros:
if alt_form.get("strip_zeros", self.strip_zeros):
if 'e' in value:
mantissa, exponent = value.split('e')
if '.' in mantissa:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def test_format():
assert '{:.0p}'.format(q) == '1420405752 Hz'
assert '{:#.0q}'.format(q) == '1 GHz'
assert '{:#.0p}'.format(q) == '1420405752. Hz'
assert '{:#.0f}'.format(q) == '1420405752.'
assert '{:#.3f}'.format(q) == '1420405751.786'
assert '{:.0g}'.format(q) == '1e+09'
assert '{:#.0g}'.format(q) == '1.e+09'
assert '{:#.3g}'.format(q) == '1.420e+09'

q = Quantity('2ns')
assert float(q) == 2e-9
Expand Down

0 comments on commit 5e48f7f

Please sign in to comment.