fix afterpoint of numbers with thousand separators resulting in correct decimal alignment#152
Merged
astanin merged 2 commits intoastanin:masterfrom May 30, 2022
Conversation
…ct decimal alignment
jamescooke
reviewed
Dec 3, 2021
Co-authored-by: James Cooke <[email protected]>
|
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.20Using 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 |
3 tasks
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 #130. It arrises as the function
_afterpointfails to recognize numbers with thousand separators. This happens because the function_afterpointrelies on the function_isnumberwhich relies on the built-in functionfloat.__init__which fails on thousand-separated numbers.I did not change the function
_isnumberas 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
_afterpointby checking not only_isnumberbut also a new function__isnumber_with_thousands_separatorwhich validates floats or ints with thousand separators in [byte]string form. This is done by regexr"^(([+-]?[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.