From 15c1b2eb36b2513d71957bccba0b71c46da26918 Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:10:10 +0530 Subject: [PATCH 1/2] Keep intcomma floats out of scientific notation Enhance number formatting to handle scientific notation. --- src/humanize/number.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..a06a6bf6 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -5,6 +5,7 @@ __lazy_modules__ = {"bisect"} import bisect +from decimal import Decimal from .i18n import _gettext as _ from .i18n import _gettext_noop as N_ @@ -198,6 +199,12 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: result = f"{value:,.{ndigits}f}" else: result = f"{value:,}" + if isinstance(value, float) and ("e" in result or "E" in result): + # repr-style float formatting switches to scientific notation + # (>= 1e16, < 1e-4); expand it so digits are still grouped. + result = format(Decimal(repr(value)), ",f") + if value.is_integer(): + result += ".0" if thousands_sep != "," or decimal_sep != ".": result = result.translate(str.maketrans(",.", thousands_sep + decimal_sep)) return result From 2e55e12e63f607dbb54fa135b3116963943dd96e Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:10:59 +0530 Subject: [PATCH 2/2] Add intcomma regression tests for scientific-notation floats Added test cases for floats in scientific notation to ensure correct formatting. --- tests/test_number.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..a93d1b02 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -75,6 +75,14 @@ def test_ordinal(test_input: str, expected: str) -> None: ([-math.inf], "-Inf"), (["nan"], "NaN"), (["-inf"], "-Inf"), + # Floats whose repr uses scientific notation stay expanded (issue #431) + ([1e16], "10,000,000,000,000,000.0"), + ([1.5e20], "150,000,000,000,000,000,000.0"), + ([-1e16], "-10,000,000,000,000,000.0"), + ([1e16, 0], "10,000,000,000,000,000"), + ([0.00001], "0.00001"), + ([1.23e-5], "0.0000123"), + ([1234.5e15], "1,234,500,000,000,000,000.0"), ], ) def test_intcomma(