Skip to main content

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:

  • even with a thousands separator, at a glance you might easily mistake a billion for a trillion

  • even when rounding, an amount like 12,214.17 dollars is a lot of number noise for communicating 12.2K

  • scientific notation leads to exponents like 1.22e4 which are hard to interpret because we’re used to working with thousands, millions and billions – orders of magnitudes that are multiples of three

  • when comparing multiple measurements of the same underlying variable, like the yearly sales numbers for 2010-2015, it’s annoying to have some numbers in thousands and other numbers in millions – you want consistency so that digits in the same position are of the same magnitude

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)
  • precision: the amount of significant digits. When necessary, business will round beyond the decimal sign as well: in the example above, 1175125.2 was turned into 1,180K rather than 1,175K to retain only 3 significant digits.

  • prefix: whether to use SI prefixes like m (milli), K (kilo) and so on instead of scientific exponents like E+03.

  • prefixes: a mapping of orders of magnitude to prefixes, e.g. {-3: 'm', 3: 'K'}, allowing you to customize the prefixes, for example using B for billion instead of T for tera.

  • statistic: a function to produce the reference number. The reference number determines the order of magnitude and precision for the entire group of numbers, so that for example when the reference number is 23.3K, smaller numbers like 1.1K won’t gain a decimal place and larger numbers like 1,180K won’t jump an order of magnitude to 1.18M. The median often works well, but if you want more precision for small outliers, try ballpark.statistics.Q1 or even Python’s builtin min.

Metadata

Release files for ballpark-fixed 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ballpark-fixed 1.4.1
File Size Uploaded Provenance
ballpark-fixed-1.4.1.tar.gz 7.6 kB Without provenance Details

Release files / ballpark-fixed-1.4.1.tar.gz

Download URL ballpark-fixed-1.4.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5b193f2b8d10b7f3c1d70bd61922b4aff1dd186397b8b120176b96cf2c5e69ab
BLAKE2b-256 checksum
How to use checksums
0da699591f19c548209dbc70af5564143094832ef504075b9df176c169df3f1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.4

Security for ballpark-fixed 1.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.4.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page