Skip to content

Fix decimal alignment for comma-grouped floats (fixes #248) - #446

Open
IMGillusion wants to merge 1 commit into
astanin:masterfrom
IMGillusion:fix/248-afterpoint-strip
Open

Fix decimal alignment for comma-grouped floats (fixes #248)#446
IMGillusion wants to merge 1 commit into
astanin:masterfrom
IMGillusion:fix/248-afterpoint-strip

Conversation

@IMGillusion

Copy link
Copy Markdown

Fixes #248.

Repro (tabulate master):

>>> from tabulate import tabulate
>>> print(tabulate([[34454634.345], [3.0]], headers=['doesnt work'],
...                 floatfmt='17,.4f', tablefmt='simple'))
           doesnt work
----------------------
  34,454,634.3450
                3.0000

The decimal points are not aligned, while the same table with
floatfmt='17.4f' (no comma group) aligns correctly.

Root cause: floatfmt='17,.4f' produces values with leading spaces
(' 34,454,634.3450'). In decimal alignment, _afterpoint() decides
how many symbols each cell has after the decimal point, but
_isnumber_with_thousands_separator() does not accept leading
whitespace, so the comma-grouped value was reported as having no
decimal point (-1) and got maxdecimals + 1 extra trailing spaces,
shifting its decimal point to the right of the others.

The fix strips the string once before the number checks (1 line in
_afterpoint), plus a doctest and a regression test asserting the
decimal points of comma-grouped floats line up.

Full suite: 383 passed, 1 skipped (pytest with --doctest-modules,
including README.md doctests).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

floatfmt with a padded number and comma separate doesn't align around decimal place

1 participant