feat: round_value should support None

This commit is contained in:
Matthias
2025-12-02 07:10:33 +01:00
parent a013793b2f
commit 0f5427f4a0
2 changed files with 4 additions and 2 deletions

View File

@@ -23,7 +23,7 @@ def strip_trailing_zeros(value: str) -> str:
return value.rstrip("0").rstrip(".") return value.rstrip("0").rstrip(".")
def round_value(value: float, decimals: int, keep_trailing_zeros=False) -> str: def round_value(value: float | None, decimals: int, keep_trailing_zeros=False) -> str:
""" """
Round value to given decimals Round value to given decimals
:param value: Value to be rounded :param value: Value to be rounded
@@ -31,7 +31,7 @@ def round_value(value: float, decimals: int, keep_trailing_zeros=False) -> str:
:param keep_trailing_zeros: Keep trailing zeros "222.200" vs. "222.2" :param keep_trailing_zeros: Keep trailing zeros "222.200" vs. "222.2"
:return: Rounded value as string :return: Rounded value as string
""" """
if isnan(value): if value is None or isnan(value):
return "N/A" return "N/A"
val = f"{value:.{decimals}f}" val = f"{value:.{decimals}f}"
if not keep_trailing_zeros: if not keep_trailing_zeros:

View File

@@ -57,6 +57,8 @@ def test_round_value():
assert round_value(222.2, 0, True) == "222" assert round_value(222.2, 0, True) == "222"
assert round_value(float("nan"), 0, True) == "N/A" assert round_value(float("nan"), 0, True) == "N/A"
assert round_value(float("nan"), 10, True) == "N/A" assert round_value(float("nan"), 10, True) == "N/A"
assert round_value(None, 10, True) == "N/A"
assert round_value(None, 1, True) == "N/A"
def test_format_duration(): def test_format_duration():