debrouwere / python-ballpark

Better human-readable numbers
ISC License
40 stars 14 forks source link

Ballpark

When people think of human-readable numbers, they think of rounding to two decimal places and adding a thousands separator. 12,214.17 is already quite an improvement over 12214.16666667. But standard formats for human-readable numbers still have various flaws:

python-ballpark introduces business notation, an offshoot of engineering notation, for producing better human-readable numbers.

Install with pip install ballpark or pip3 install ballpark.

What it looks like

numbers rounded engineering notation business notation
11234.22, 233000.55, 1175125.2 11,234.22, 233,000.55, 1,175,125.2 11.2E+3, 233E+3, 1.18E+6 11K, 233K, 1,180K
111, 1111.23, 1175125.234 111, 1,111.23, 1,175,125.23 111, 1.11E+3, 1.18E+6 0.11K, 1.11K, 1,180.00K

How to use it

>>> from ballpark import human, scientific, engineering, business, ballpark
>>> business([11234.22, 233000.55, 1175125.2])
['11K', '233K', '1,180K']
>>>
>>> # business notation is also aliased as `ballpark`
>>> ballpark([11234.22, 233000.55, 1175125.2])
['11K', '233K', '1,180K']
>>>
>>> # or use the shortcut functions
>>> from ballpark import H, S, E, B
>>> B([11234.22, 233000.55, 1175125.2])
['11K', '233K', '1,180K']
>>>
>>> # all notations accept single numbers too, but then we can't guarantee
>>> # that all numbers will have the same prefix (kilo, mega etc.)
>>> [B(value) for value in [11234.22, 233000.55, 1175125.2]]
['11.2K', '233K', '1.18M']

How it works

business(values, precision=3, prefix=True, prefixes=SI, statistic=median)