diff --git a/tests/test_items.py b/tests/test_items.py index cf6134aa..e814b5f6 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -1451,6 +1451,19 @@ def test_no_extra_minus_sign() -> None: assert doc.as_string() == "a = -1.5" +def test_no_extra_plus_sign_on_negative_zero() -> None: + doc = parse("a = -1.5") + doc["a"] *= 0 + assert doc.as_string() == "a = -0.0" + doc["a"] *= -1 + assert doc.as_string() == "a = +0.0" + + # Integer true division also goes through Float._new. + doc = parse("a = +0") + doc["a"] /= -1 + assert doc.as_string() == "a = -0.0" + + def test_serialize_table_with_dotted_key() -> None: child = api.table() child.add(api.key(("b", "c")), 1) diff --git a/tomlkit/items.py b/tomlkit/items.py index 8589df97..db151f7b 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -876,7 +876,8 @@ def as_string(self) -> str: def _new(self, result: float) -> Float: raw = str(result) - if self._sign and result >= 0: + # -0.0 >= 0 is True, but str(-0.0) already carries its sign. + if self._sign and result >= 0 and not raw.startswith("-"): raw = f"+{raw}" return Float(result, self._trivia, raw)