A Python library to print numbers in human readable format
Project description
readable-number
A Python library to print numbers in human readable format
1. Installation
pip install readable-number
This library does not depend on any third-party libraries, so installing it will not break your Python environment.
2. Usage
2.1. Import and basic usage
from readable_number import ReadableNumber
str(ReadableNumber(-1.23)) # -1.23
str(ReadableNumber(-123)) # -123
Print many numbers with the same config:
rn = ReadableNumber(precision=2, digit_group_size=4)
rn.of(123456789) # 1,2345,6789
rn.of(0.123456789) # 0.12
rn.of(1e15) # 1000,0000,0000,0000
2.2. Print large integers
2.2.1. Print in groups
str(ReadableNumber(-123)) # -123
str(ReadableNumber(-1234)) # -1,234
str(ReadableNumber(-123456789)) # -123,456,789
str(ReadableNumber(-12345.6789)) # -12,345.6789
str(ReadableNumber(-1.23456e18)) # -1,234,560,000,000,000,000
2.2.2. Custom grouping (in locales other than en-US)
str(ReadableNumber(-123456789, digit_group_size=4)) # -1,2345,6789
str(ReadableNumber(-123456789, digit_group_delimiter='|')) # -123|456|789
2.2.3. Print in shortform
str(ReadableNumber(12345, use_shortform=True)) # 12k
str(ReadableNumber(12345, use_shortform=True, precision=1)) # 12.3k
str(ReadableNumber(12345678, use_shortform=True)) # 12M
str(ReadableNumber(12345678, use_shortform=True, precision=2)) # 12.35M
str(ReadableNumber(1234567890, use_shortform=True, precision=2)) # 1.23B
2.3. Setting precision or significant figures
2.3.1. Without setting precision or significant figures
Numbers are printed in a "natural" way:
str(ReadableNumber(0.12345)) # 0.12345
str(ReadableNumber(0.0000012345)) # 0.0000012345
2.3.2. Precision
str(ReadableNumber(0.12345, precision=None)) # 0.12345
str(ReadableNumber(0.12345, precision=2)) # 0.12
str(ReadableNumber(0.12345, precision=20)) # 0.123450000000000
2.3.3. Significant figures
str(ReadableNumber(0.12345, significant_figures_after_decimal_point=3)) # 0.123
str(ReadableNumber(12345, significant_figures_after_decimal_point=3)) # 12,345
str(ReadableNumber(0.00012345, significant_figures_after_decimal_point=3)) # 0.000123
str(ReadableNumber(-1.2345e-50, significant_figures_after_decimal_point=3))
# -0.0000000000000000000000000000000000000000000000000123
2.4. Exponential notations
2.4.1. Print large numbers in exponential notation
str(ReadableNumber(1234567890, use_exponent_for_large_numbers=True)) # 1.234568e+09
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
precision=2,
)) # 1.23e+09
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
precision=None,
)) # 1.23456789e+09
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
large_number_threshold=1e20, # only show in exp if we exceed this
precision=None,
)) # 1,234,567,890
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
large_number_threshold=1e20, # only show in exp if we exceed this
significant_figures_after_decimal_point=5,
)) # 1.2346e+09
2.4.2. Print small numbers in exponential notation
str(ReadableNumber(0.000000012, use_exponent_for_small_numbers=True)) # 1.200000e-08
str(ReadableNumber(
-0.000000123456,
use_exponent_for_small_numbers=True,
precision=2,
)) # -1.23e-07
str(ReadableNumber(
-0.000012345,
use_exponent_for_small_numbers=True,
precision=None,
small_number_threshold=1e-2,
)) # -1.2345e-05
str(ReadableNumber(
-0.00000012345,
use_exponent_for_small_numbers=True,
significant_figures_after_decimal_point=2,
)) # -1.23e-07
3. Full API documentation
Please visit this site: https://readable-number.readthedocs.io/en/stable/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
readable_number-0.1.3.tar.gz
(13.8 kB
view details)
Built Distribution
File details
Details for the file readable_number-0.1.3.tar.gz
.
File metadata
- Download URL: readable_number-0.1.3.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75b6849ad0177d63e5ae8ba735d29c776853d1fe9a0678ea7a3e32d0b7464961 |
|
MD5 | b014f531b80f5449c238ffa70708f1e2 |
|
BLAKE2b-256 | 9e80a9dc523cef80a01b5a0890f96bd24de19968258d91e390f85178893b960a |
File details
Details for the file readable_number-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: readable_number-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 902680e70fac3433d0cdc4a72fd3dbbdd05eb4cea516014f509bd28d58f9611a |
|
MD5 | 722d81b74500282ce3f109f91c237bda |
|
BLAKE2b-256 | 4b62e5632a1a6cfbaa141ab8438864c7289808cedf41cee69164c5bb9ea659cc |