Skip to content

Commit

Permalink
fix: Fix round function returning float instead of int (#2089)
Browse files Browse the repository at this point in the history
Fix #2081
  • Loading branch information
jules-ch authored Dec 10, 2024
1 parent c70c076 commit b436902
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Pint Changelog
-------------------

- Add docs to the functions in ``pint.testing`` (PR #2070)
- Fix round function returning float instead of int (#2081)


0.24.4 (2024-11-07)
Expand Down
12 changes: 6 additions & 6 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
pint.facets.plain.quantity
~~~~~~~~~~~~~~~~~~~~~~~~~
pint.facets.plain.quantity
~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: 2022 by Pint Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
:copyright: 2022 by Pint Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import annotations
Expand Down Expand Up @@ -1288,8 +1288,8 @@ def __rpow__(self, other) -> PlainQuantity[MagnitudeT]:
def __abs__(self) -> PlainQuantity[MagnitudeT]:
return self.__class__(abs(self._magnitude), self._units)

def __round__(self, ndigits: int | None = 0) -> PlainQuantity[MagnitudeT]:
return self.__class__(round(self._magnitude, ndigits=ndigits), self._units)
def __round__(self, ndigits: int | None = None) -> PlainQuantity[int]:
return self.__class__(round(self._magnitude, ndigits), self._units)

def __pos__(self) -> PlainQuantity[MagnitudeT]:
return self.__class__(operator.pos(self._magnitude), self._units)
Expand Down
5 changes: 5 additions & 0 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def test_quantity_creation(self, caplog):
assert 4.2 * self.ureg.meter == self.Q_(4.2, 2 * self.ureg.meter)
assert len(caplog.records) == 1

def test_round(self):
x = self.Q_(1.1, "kg")
assert isinstance(round(x).magnitude, int)
assert isinstance(round(x, 0).magnitude, float)

def test_quantity_with_quantity(self):
x = self.Q_(4.2, "m")
assert self.Q_(x, "m").magnitude == 4.2
Expand Down

0 comments on commit b436902

Please sign in to comment.