classification
Title: pprint numbers with underscore
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, fov, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-01-12 23:21 by fov, last changed 2021-01-13 15:04 by serhiy.storchaka.

Messages (5)
msg384982 - (view) Author: Felipe (fov) * Date: 2021-01-12 23:21
It would be nice if pprint learned to insert underscores in long numbers

Current behavior:

>>> pprint.pprint(int(1e9))
1000000000

Desired behavior

>>> pprint.pprint(int(1e9))
1_000_000_000

Wikipedia tells me that "groups of 3" is the international standard to be followed here [1][2]

[1] https://en.wikipedia.org/wiki/ISO_31-0#Numbers
[2] https://en.wikipedia.org/wiki/Decimal_separator#Digit_grouping
msg384985 - (view) Author: Felipe (fov) * Date: 2021-01-12 23:50
Here is an implementation of the safe repr for numbers if helpful:

```
def safe_repr_int(object):
    sign = ''
    if object < 0:
        sign = '-'
        object  = -object
    r = repr(object)
    if len(r) <= 4:
        return sign + r
    parts = [sign]
    left = len(r) % 3
    if left:
        parts.append(r[0:left])
        parts.append('_')
        r = r[left:]
    parts.append(r[0:3])
    for i in range(3, len(r), 3):
        parts.append('_')
        parts.append(r[i:i + 3])
    return ''.join(parts)
```
msg384992 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-01-13 03:35
> It would be nice if pprint learned to insert underscores in long numbers

+1 but I would make this optional.


> Here is an implementation of the safe repr for numbers if helpful

I suggest using the existing string formatting tools as a foundation

    >>> format(10**9, ',d').replace(',', '_')
    '1_000_000_000'
msg385025 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-01-13 13:37
+1 also. I agree with Raymond it should be optional.
msg385034 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-01-13 15:04
>>> format(10**9, '_d')
'1_000_000_000'
History
Date User Action Args
2021-01-13 15:04:56serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg385034
2021-01-13 13:37:23eric.smithsetnosy: + eric.smith
messages: + msg385025
2021-01-13 03:35:29rhettingersetnosy: + rhettinger
messages: + msg384992
2021-01-12 23:50:25fovsetmessages: + msg384985
2021-01-12 23:21:21fovcreate