Fix decimal alignment for comma-grouped floats (fixes #248) - #446
Open
IMGillusion wants to merge 1 commit into
Open
Fix decimal alignment for comma-grouped floats (fixes #248)#446IMGillusion wants to merge 1 commit into
IMGillusion wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #248.
Repro (tabulate master):
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()decideshow many symbols each cell has after the decimal point, but
_isnumber_with_thousands_separator()does not accept leadingwhitespace, so the comma-grouped value was reported as having no
decimal point (
-1) and gotmaxdecimals + 1extra 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 thedecimal points of comma-grouped floats line up.
Full suite: 383 passed, 1 skipped (pytest with --doctest-modules,
including README.md doctests).