Skip to content

Commit cc10827

Browse files
committed
Fix missing number in Quantity formatting for small values
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent b425905 commit cc10827

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@
8787
A bug made the resampler interpret zero values as `None` when generating new samples, so if the result of the resampling is zero, the resampler would just produce `None` values.
8888

8989
- The PowerManager no longer holds on to proposals from dead actors forever. If an actor hasn't sent a new proposal in 60 seconds, the available proposal from that actor is dropped.
90+
- Fix `Quantity.__format__()` sometimes skipping the number for very small values.

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ def __format__(self, __format_spec: str) -> str:
226226
else:
227227
unit = self._exponent_unit_map[unit_place]
228228
value_str = f"{self._base_value / 10 ** unit_place:.{precision}f}"
229-
stripped = value_str.rstrip("0").rstrip(".")
229+
230+
if value_str != "0":
231+
stripped = value_str.rstrip("0").rstrip(".")
232+
else:
233+
stripped = value_str
234+
230235
if not keep_trailing_zeros:
231236
value_str = stripped
232237
unit_str = unit if stripped != "0" else self._exponent_unit_map[0]

tests/timeseries/test_quantities.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def test_string_representation() -> None:
107107
)
108108
assert f"{Quantity(1.024445, exponent=0)}" == "1.024"
109109
assert f"{Quantity(1.024445, exponent=0):.0}" == "1"
110+
assert f"{Quantity(0.124445, exponent=0):.0}" == "0"
111+
assert f"{Quantity(0.50001, exponent=0):.0}" == "1"
110112
assert f"{Quantity(1.024445, exponent=0):.6}" == "1.024445"
111113

112114
assert f"{Quantity(1.024445, exponent=3)}" == "1024.445"
@@ -128,7 +130,6 @@ def test_string_representation() -> None:
128130

129131
assert f"{Fz1(1.024445, exponent=6)}" == "1024.445 kHz"
130132
assert f"{Fz2(1.024445, exponent=6)}" == "1.024 MHz"
131-
132133
assert f"{Fz1(1.024445, exponent=9)}" == "1024445 kHz"
133134
assert f"{Fz2(1.024445, exponent=9)}" == "1.024 GHz"
134135

@@ -147,6 +148,9 @@ def test_string_representation() -> None:
147148
assert f"{Fz1(-20)}" == "-20 Hz"
148149
assert f"{Fz1(-20000)}" == "-20 kHz"
149150

151+
assert f"{Power.from_watts(0.000124445):.0}" == "0 W"
152+
assert f"{Energy.from_watt_hours(0.124445):.0}" == "0 Wh"
153+
150154

151155
def test_isclose() -> None:
152156
"""Test the isclose method of the quantities."""

0 commit comments

Comments
 (0)