From a922133032c377bee47148b60404df85b93422e4 Mon Sep 17 00:00:00 2001 From: IMGillusion Date: Wed, 9 Sep 2026 15:59:13 +0800 Subject: [PATCH] Fix decimal alignment for comma-grouped floats (issue #248) When a floatfmt uses a thousands separator with a width (e.g. '17,.4f'), the formatted values carry leading spaces. _afterpoint() then failed to recognize the comma-grouped value as a number (its regex does not allow leading whitespace), so its decimal length was reported as -1 and the decimal-alignment padding put the decimal points in different columns. Strip the string before the number checks. No behavior change for values without thousands separators. --- tabulate/__init__.py | 3 +++ test/test_regression.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..d3aaafb 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1021,8 +1021,11 @@ def _afterpoint(string): 2 >>> _afterpoint("123,456.78") 2 + >>> _afterpoint(" 123,456.78 ") + 2 """ + string = string.strip() if _isnumber(string) or _isnumber_with_thousands_separator(string): if _isint(string): return -1 diff --git a/test/test_regression.py b/test/test_regression.py index 9555676..4e5107c 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -211,6 +211,16 @@ def test_column_with_mixed_value_types(): assert_equal(table, expected) +def test_decimal_alignment_with_thousands_separator(): + "Regression: decimal alignment must work for comma-grouped floats (issue #248)" + result = tabulate([[34454634.345], [3.0]], headers=["doesnt work"], floatfmt="17,.4f", tablefmt="simple") + rows = [row for row in result.splitlines() if "." in row and any(c.isdigit() for c in row)] + assert len(rows) == 2 + # the decimal points of both rows must sit in the same column + dot_cols = {row.index(".") for row in rows} + assert len(dot_cols) == 1 + + def test_latex_escape_special_chars(): "Regression: escape special characters in LaTeX output (issue #32)" expected = "\n".join(