python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

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

Open airvzxf opened this issue 10 months ago • 0 comments

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 format_spec. The interpretation of format_spec 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.

airvzxf avatar Apr 14 '24 20:04 airvzxf