Skip to content

Commit 493df59

Browse files
committed
Fix quantity formatting bug with -0/0
It would only print the unit, no number. Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 1123f46 commit 493df59

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,17 @@ def __format__(self, __format_spec: str) -> str:
261261
unit_place = max(self._exponent_unit_map)
262262
else:
263263
unit = self._exponent_unit_map[unit_place]
264+
264265
value_str = f"{self._base_value / 10 ** unit_place:.{precision}f}"
265266

266-
if value_str != "0":
267-
stripped = value_str.rstrip("0").rstrip(".")
268-
else:
267+
if value_str in ("-0", "0"):
269268
stripped = value_str
269+
else:
270+
stripped = value_str.rstrip("0").rstrip(".")
270271

271272
if not keep_trailing_zeros:
272273
value_str = stripped
273-
unit_str = unit if stripped != "0" else self._exponent_unit_map[0]
274+
unit_str = unit if stripped not in ("-0", "0") else self._exponent_unit_map[0]
274275
return f"{value_str} {unit_str}"
275276

276277
def __add__(self, other: Self) -> Self:

tests/timeseries/test_quantities.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def test_string_representation() -> None:
152152

153153
assert f"{Power.from_watts(0.000124445):.0}" == "0 W"
154154
assert f"{Energy.from_watt_hours(0.124445):.0}" == "0 Wh"
155+
assert f"{Power.from_watts(-0.0):.0}" == "-0 W"
156+
assert f"{Power.from_watts(0.0):.0}" == "0 W"
155157

156158

157159
def test_isclose() -> None:

0 commit comments

Comments
 (0)