Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/frequenz/quantities/_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ def base_value(self) -> float:
"""
return self._base_value

def __round__(self, ndigits: int | None = None) -> Self:
"""Round this quantity to the given number of digits.

Args:
ndigits: The number of digits to round to.

Returns:
The rounded quantity.
"""
return self._new(round(self._base_value, ndigits))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blankline after function?

@property
def base_unit(self) -> str | None:
"""Return the base unit of this quantity.
Expand Down
5 changes: 5 additions & 0 deletions tests/test_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def test_isclose() -> None:
assert not Fz1(1.024445).isclose(Fz1(1.0))


@hypothesis.given(value=st.floats(allow_nan=False, allow_infinity=False))
@pytest.mark.parametrize("ndigits", [0, 1, 2, 3])
def test_round(value: float, ndigits: int) -> None:
"""Test the rounding of the quantities."""
assert round(Quantity(value), ndigits) == Quantity(round(value, ndigits))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a blnak line here?

def test_addition_subtraction() -> None:
"""Test the addition and subtraction of the quantities."""
assert Quantity(1) + Quantity(1, exponent=0) == Quantity(2, exponent=0)
Expand Down