From 9019549a119a2c6072f890383ded6d66ce18acd7 Mon Sep 17 00:00:00 2001 From: Ken Kundert Date: Fri, 1 Mar 2024 22:04:56 -0800 Subject: [PATCH] fix another issue in format() --- quantiphy/quantiphy.py | 14 ++++++-------- tests/test_format.py | 8 ++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/quantiphy/quantiphy.py b/quantiphy/quantiphy.py index 4fbaf87..f3d4045 100644 --- a/quantiphy/quantiphy.py +++ b/quantiphy/quantiphy.py @@ -2576,6 +2576,7 @@ def format(self, template=''): 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 {} if ftype and ftype in 'dnu': if ftype == 'u': value = scale if scale else self.units @@ -2591,30 +2592,27 @@ def format(self, template=''): if ftype in 's': # note that ftype = '' matches this case label = label if ftype else None value = self.render( - prec=prec, show_label=label, scale=scale, - strip_zeros=not alt_form, strip_radix=not alt_form + prec=prec, show_label=label, scale=scale, **alt_form ) elif ftype == 'q': value = self.render( form='si', prec=prec, show_units=True, show_label=label, - strip_zeros=not alt_form, strip_radix=not alt_form, scale=scale + scale=scale, **alt_form ) elif ftype == 'r': value = self.render( form='si', prec=prec, show_units=False, show_label=label, - strip_zeros=not alt_form, strip_radix=not alt_form, scale=scale + scale=scale, **alt_form ) elif ftype == 'p': value = self.fixed( prec=prec, show_units=True, show_label=label, - show_commas=bool(comma), strip_zeros=not alt_form, - strip_radix=not alt_form, scale=scale + show_commas=bool(comma), scale=scale, **alt_form ) elif ftype == 'b': value = self.binary( prec=prec, show_units=True, show_label=label, - strip_zeros=not alt_form, strip_radix=not alt_form, - scale=scale + scale=scale, **alt_form ) else: if prec is None: diff --git a/tests/test_format.py b/tests/test_format.py index 860ccb7..8ef7711 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -399,10 +399,10 @@ def test_format_method(): assert q.format('G') == 'f = 1.4204e+09' assert q.format('n') == 'f' assert q.format('d') == 'frequency of hydrogen line' - assert q.format('p') == '1420405751.786 Hz' - assert q.format(',p') == '1,420,405,751.786 Hz' - assert q.format('P') == 'f = 1420405751.786 Hz' - assert q.format(',P') == 'f = 1,420,405,751.786 Hz' + assert q.format('p') == '1420405751.7860 Hz' + assert q.format(',p') == '1,420,405,751.7860 Hz' + assert q.format('P') == 'f = 1420405751.7860 Hz' + assert q.format(',P') == 'f = 1,420,405,751.7860 Hz' assert q.format('#p') == '1420405751.7860 Hz' assert q.format('#,p') == '1,420,405,751.7860 Hz' assert q.format('#P') == 'f = 1420405751.7860 Hz'