astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.08k stars 162 forks source link

Feature: for numbers, apply the “digit grouping” for integers and floats. #320

Open airvzxf opened 4 months ago

airvzxf commented 4 months ago

Feature: for numbers, apply the “digit grouping” for integers and floats.

Context

Decimal separator

A decimal separator is a symbol used to separate the integer part from the fractional part of a number written in decimal form (e.g., “.” in 12.45).

https://en.wikipedia.org/wiki/Decimal_separator

Digit grouping

In general, digits should be grouped and separated either by commas or by narrow gaps (never a period/full point).

https://en.wikipedia.org/wiki/Wikipedia:Manual_of_Style/Dates_and_numbers#Grouping_of_digits

Python

format(value, format_spec='')

Convert a value to a “formatted” representation, as controlled by _formatspec. The interpretation of _formatspec will depend on the type of the value argument; however, there is a standard formatting syntax that is used by most built-in types: Format Specification Mini-Language.

https://docs.python.org/3.11/library/functions.html#format

Format Specification Mini-Language

“Format specifications” are used within replacement fields contained within a format string to define how individual values are presented.

grouping_option ::= "_" | ","

https://docs.python.org/3.11/library/string.html#formatspec

Description

Currently, “tabulate” makes a distinction between integers and floats. However, the “intfmt” parameter applies the “digit grouping” format exclusively to integers, excluding floats. This prevents a decimal containing more than 3 numbers from being grouped. For example, the number “1234567.89” should be displayed in this format: “1,234,567.89”.

Suggestion

Implement functionality to perform “digit grouping”, regardless of whether the column type is a float or integer. This includes changing the “intfmt” parameter to “digitgrp” or something similar. In the source code, if it is an integer or float number, the format assigned in the “digitgrp” parameter would be applied to it.

Conflicts

Applying this feature will be in conflict with the issue https://github.com/astanin/python-tabulate/issues/318. I suggest to first finishing and merging the solutions related to this issue, then working on this feature.