diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index d6cc0657d..fae1a43dd 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -275,7 +275,8 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: count: int | str = 0 if self.microseconds != 0: unit = f"units.second.{loaded_locale.plural(0)}" - count = f"{abs(self.microseconds) / 1e6:.2f}" + value = round(self.microseconds / 1e6, 2) + count = f"{value + 0.0:.2f}" # +0.0 turns -0.0 into 0.0 else: unit = f"units.microsecond.{loaded_locale.plural(0)}" translation = loaded_locale.translation(unit) diff --git a/src/pendulum/interval.py b/src/pendulum/interval.py index c5e0713a7..d98e47863 100644 --- a/src/pendulum/interval.py +++ b/src/pendulum/interval.py @@ -278,7 +278,8 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: count: str | int = 0 if abs(self.microseconds) > 0: unit = f"units.second.{loaded_locale.plural(1)}" - count = f"{abs(self.microseconds) / 1e6:.2f}" + value = round(self.microseconds / 1e6, 2) + count = f"{value + 0.0:.2f}" # +0.0 turns -0.0 into 0.0 else: unit = f"units.microsecond.{loaded_locale.plural(0)}" diff --git a/tests/duration/test_in_words.py b/tests/duration/test_in_words.py index 13982c33d..0ca557f80 100644 --- a/tests/duration/test_in_words.py +++ b/tests/duration/test_in_words.py @@ -65,6 +65,18 @@ def test_subseconds(): assert pi.in_words() == "0.12 seconds" +def test_negative_subseconds(): + pi = pendulum.duration(microseconds=-123456) + + assert pi.in_words() == "-0.12 seconds" + + +def test_negative_subseconds_round_to_zero(): + pi = pendulum.duration(microseconds=-1000) + + assert pi.in_words() == "0.00 seconds" + + def test_subseconds_with_seconds(): pi = pendulum.duration(seconds=12, microseconds=123456) assert pi.in_words() == "12 seconds" diff --git a/tests/interval/test_in_words.py b/tests/interval/test_in_words.py index f60e20d04..b4183bc49 100644 --- a/tests/interval/test_in_words.py +++ b/tests/interval/test_in_words.py @@ -56,6 +56,22 @@ def test_subseconds(): assert interval.in_words() == "0.12 second" +def test_negative_subseconds(): + start_date = pendulum.datetime(2012, 1, 1) + interval = pendulum.interval( + start=start_date.add(microseconds=123456), end=start_date + ) + assert interval.in_words() == "-0.12 second" + + +def test_negative_subseconds_round_to_zero(): + start_date = pendulum.datetime(2012, 1, 1) + interval = pendulum.interval( + start=start_date.add(microseconds=1000), end=start_date + ) + assert interval.in_words() == "0.00 second" + + def test_subseconds_with_seconds(): start_date = pendulum.datetime(2012, 1, 1) interval = pendulum.interval(