You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/09/29 03:39:43 UTC

(superset) branch master updated: fix(reports): drop the sign of negative numbers that round to zero in text reports (#44644)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new c67c88fbf1e fix(reports): drop the sign of negative numbers that round to zero in text reports (#44644)
c67c88fbf1e is described below

commit c67c88fbf1e2d762e12ad6e850d884a728bb35d0
Author: Devansh Bhargava <90...@users.noreply.github.com>
AuthorDate: Tue Sep 29 09:09:36 2026 +0530

    fix(reports): drop the sign of negative numbers that round to zero in text reports (#44644)
---
 superset/utils/number_format.py              | 10 +++++++---
 tests/unit_tests/utils/number_format_test.py | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/superset/utils/number_format.py b/superset/utils/number_format.py
index 66e016ec0ff..99fccfbefa2 100644
--- a/superset/utils/number_format.py
+++ b/superset/utils/number_format.py
@@ -327,12 +327,16 @@ def apply_sign(formatted: str, value: float, sign_mode: str) -> str:
 
     Negative values get a leading ``-`` (or wrapping parentheses for the ``(``
     accounting mode); positive values get a ``+`` or a leading space only for the
-    ``+`` and space modes respectively. Mirrors the sign decoration in
-    ``d3-format/src/locale.js``.
+    ``+`` and space modes respectively. A negative value (including ``-0.0``)
+    whose formatted magnitude rounds to zero is shown unsigned unless the ``+``
+    mode is requested. Mirrors the sign decoration in ``d3-format/src/locale.js``.
 
     :return: the signed or accounting-decorated string
     """
-    if value < 0:
+    negative = math.copysign(1.0, value) < 0
+    if negative and sign_mode != "+" and not re.search(r"[1-9]", formatted):
+        negative = False
+    if negative:
         return f"({formatted})" if sign_mode == "(" else f"-{formatted}"
     if sign_mode == "+":
         return f"+{formatted}"
diff --git a/tests/unit_tests/utils/number_format_test.py b/tests/unit_tests/utils/number_format_test.py
index 09eecdf10cd..30369e8ee9a 100644
--- a/tests/unit_tests/utils/number_format_test.py
+++ b/tests/unit_tests/utils/number_format_test.py
@@ -514,6 +514,32 @@ def test_additional_d3_parity_cases(
     assert format_number_with_config(d3_format, None, value) == expected
 
 
+@pytest.mark.parametrize(
+    "d3_format,value,expected",
+    [
+        (",.2f", -0.001, "0.00"),
+        (",.2f", -0.0049, "0.00"),
+        (",.2f", -0.0, "0.00"),
+        ("(,.2f", -0.001, "0.00"),
+        ("$,.2f", -0.004, "$0.00"),
+        ("($,.2f", -0.001, "$0.00"),
+        (" ,.2f", -0.001, " 0.00"),
+        (".1%", -0.0001, "0.0%"),
+        (",d", -0.4, "0"),
+        (".0f", -0.3, "0"),
+        ("+,.2f", -0.001, "-0.00"),
+        ("+,.2f", -0.0, "-0.00"),
+        ("+,d", -0.4, "-0"),
+        (".2f", -0.005, "-0.01"),
+        ("~g", -1e-20, "-1e-20"),
+    ],
+)
+def test_negative_value_rounding_to_zero_matches_d3(
+    d3_format: str, value: float, expected: str
+) -> None:
+    assert format_number_with_config(d3_format, None, value) == expected
+
+
 @pytest.mark.parametrize(
     "d3_format,value,expected",
     [