Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix afterpoint of numbers with thousand separators resulting in correct decimal alignment #152

Merged
merged 2 commits into from
May 30, 2022

Conversation

alexandergazo
Copy link
Contributor

@alexandergazo alexandergazo commented Oct 14, 2021

Fixes #130. It arrises as the function _afterpoint fails to recognize numbers with thousand separators. This happens because the function _afterpoint relies on the function _isnumber which relies on the built-in function float.__init__ which fails on thousand-separated numbers.

I did not change the function _isnumber as numbers such as "123,123" are to be aligned as plain text, resulting from the issue #110 and subsequent pulls and tests (test/test_regression.py::test_string_with_comma_between_digits_without_floatfmt_grouping_option).

The solution is therefore implemented in the function _afterpoint by checking not only _isnumber but also a new function __isnumber_with_thousands_separator which validates floats or ints with thousand separators in [byte]string form. This is done by regex r"^(([+-]?[0-9]{1,3})(?:,([0-9]{3}))*)?(?(1)\.[0-9]*|\.[0-9]+)?$" which simply optionally matches signs, then the leading group, then separated thousands, and then the decimal part. A decimal part is required if the non-decimal part is empty.

The tests were implemented only for the function tabulate._align_column.

tabulate.py Outdated Show resolved Hide resolved
Co-authored-by: James Cooke <jamescooke@users.noreply.github.com>
@Erotemic
Copy link

Any chance this could be merged soon? I just ran into this issue, and it would be nice to have it fixed.

I took a look at the code and it seemed fine to me. I also checked out the branch and verified that the solution works in my case.

Using the current "main" branch:

In [2]:     import tabulate
   ...:     data = [
   ...:         [13213.2, 3213254.23, 432432.231,],
   ...:         [432432., 432.3, 3.2]
   ...:     ]
   ...:     print(tabulate.tabulate(data, headers=['a', 'b'], floatfmt=',.02f'))
   ...:     print(tabulate.tabulate(data, headers=['a', 'b'], floatfmt='.02f'))
   ...: 
                          a              b
----------  ---------------  -------------
 13,213.20  3,213,254.23     432,432.23
432,432.00           432.30           3.20
                    a          b
---------  ----------  ---------
 13213.20  3213254.23  432432.23
432432.00      432.30       3.20

Using this branch:

In [3]:     import tabulate
   ...:     data = [
   ...:         [13213.2, 3213254.23, 432432.231,],
   ...:         [432432., 432.3, 3.2]
   ...:     ]
   ...:     print(tabulate.tabulate(data, headers=['a', 'b'], floatfmt=',.02f'))
   ...:     print(tabulate.tabulate(data, headers=['a', 'b'], floatfmt='.02f'))
                       a           b
----------  ------------  ----------
 13,213.20  3,213,254.23  432,432.23
432,432.00        432.30        3.20
                    a          b
---------  ----------  ---------
 13213.20  3213254.23  432432.23
432432.00      432.30       3.20

@astanin astanin merged commit a59d1de into astanin:master May 30, 2022
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.

Float with comma formatting are mis-aligned
4 participants