Skip to content

Commit 9b1f608

Browse files
authored
Fix formatting bug for zero values (#503)
Also add tests for zero and negative values. The base branch for this PR is v0.22.x, so that we can release a patch release v0.22.1 immediately. Once this is merged, we can merge v0.22.x into v0.x.x.
2 parents 1b7053e + 3ce2807 commit 9b1f608

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def __format__(self, __format_spec: str) -> str:
164164
return f"{self._base_value:.{precision}f}"
165165

166166
abs_value = abs(self._base_value)
167-
exponent = math.floor(math.log10(abs_value))
167+
exponent = math.floor(math.log10(abs_value)) if abs_value else 0
168168
unit_place = exponent - exponent % 3
169169
if unit_place < min(self._exponent_unit_map):
170170
unit = self._exponent_unit_map[min(self._exponent_unit_map.keys())]

tests/timeseries/test_quantities.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ def test_string_representation() -> None:
8484
assert f"{Fz1(1.024445, exponent=-12)}" == "0 Hz"
8585
assert f"{Fz2(1.024445, exponent=-12)}" == "0 Hz"
8686

87+
assert f"{Fz1(0)}" == "0 Hz"
88+
89+
assert f"{Fz1(-20)}" == "-20 Hz"
90+
assert f"{Fz1(-20000)}" == "-20 kHz"
91+
8792

8893
def test_addition_subtraction() -> None:
8994
"""Test the addition and subtraction of the quantities."""

0 commit comments

Comments
 (0)