Millify
Millify is a lightweight Python library designed to convert large numbers into human-readable, "millified" formats, similar to how social media platforms display follower counts (e.g., 1,234,567 becomes 1.23M). It currently stands at version 0.1.1 and has a relatively slow release cadence, with updates focused on minor improvements and bug fixes.
Common errors
-
TypeError: 'int' object is not subscriptable
cause The input value passed to the `millify` function was not a number (e.g., a string, list, or None).fixEnsure the value passed to `millify` is an integer or a float. -
My number isn't displaying the right number of decimal places even with precision set.
cause This is often due to `decimal_places` being set simultaneously (which overrides `precision`), or `drop_zeros=True` (the default) removing trailing zeros.fixRemove the `decimal_places` argument if you are using `precision`. If you need to keep trailing zeros, set `drop_zeros=False`.
Warnings
- gotcha When both `precision` and `decimal_places` are provided to the `millify` function, `decimal_places` will take precedence, overriding the `precision` setting.
- gotcha The default prefixes (K, M, B, T for thousands, millions, billions, trillions) are based on standard SI units. For contexts requiring different scales or binary prefixes (e.g., KiB, MiB for computing), custom prefixes must be explicitly provided.
- gotcha By default, `drop_zeros=True`, meaning trailing zeros after the decimal point will be removed (e.g., `1.0M` becomes `1M`). If you need to always show a specific number of decimal places, including trailing zeros, this default behavior might be unexpected.
Install
-
pip install millify
Imports
- millify
from millify import millify
Quickstart
from millify import millify
# Basic usage: 1,234,567 -> 1.23M
print(f"Default: {millify(1234567)}")
# With specified precision: 987,654,321 -> 988M (3 significant digits)
print(f"Precision=3: {millify(987654321, precision=3)}")
# With specified decimal places: 5,000,000,000 -> 5.0B
print(f"Decimal places=1: {millify(5000000000, decimal_places=1)}")
# Custom prefixes for different scales (e.g., 'K' for 10^3, 'M' for 10^6, 'G' for 10^9)
print(f"Custom prefixes: {millify(1024 * 1024 * 5, prefixes=['', 'KB', 'MB', 'GB'], decimal_places=2)}")