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))
str(ReadableNumber(-123))
Print many numbers with the same config:
rn = ReadableNumber(precision=2, digit_group_size=4)
rn.of(123456789)
rn.of(0.123456789)
rn.of(1e15)
2.2. Print large integers
2.2.1. Print in groups
str(ReadableNumber(-123))
str(ReadableNumber(-1234))
str(ReadableNumber(-123456789))
str(ReadableNumber(-12345.6789))
str(ReadableNumber(-1.23456e18))
2.2.2. Custom grouping (in locales other than en-US)
str(ReadableNumber(-123456789, digit_group_size=4))
str(ReadableNumber(-123456789, digit_group_delimiter='|'))
2.2.3. Print in shortform
str(ReadableNumber(12345, use_shortform=True))
str(ReadableNumber(12345, use_shortform=True, precision=1))
str(ReadableNumber(12345678, use_shortform=True))
str(ReadableNumber(12345678, use_shortform=True, precision=2))
str(ReadableNumber(1234567890, use_shortform=True, precision=2))
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))
str(ReadableNumber(0.0000012345))
2.3.2. Precision
str(ReadableNumber(0.12345, precision=None))
str(ReadableNumber(0.12345, precision=2))
str(ReadableNumber(0.12345, precision=20))
2.3.3. Significant figures
str(ReadableNumber(0.12345, significant_figures_after_decimal_point=3))
str(ReadableNumber(12345, significant_figures_after_decimal_point=3))
str(ReadableNumber(0.00012345, significant_figures_after_decimal_point=3))
str(ReadableNumber(-1.2345e-50, significant_figures_after_decimal_point=3))
2.4. Exponential notations
2.4.1. Print large numbers in exponential notation
str(ReadableNumber(1234567890, use_exponent_for_large_numbers=True))
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
precision=2,
))
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
precision=None,
))
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
large_number_threshold=1e20,
precision=None,
))
str(ReadableNumber(
1234567890,
use_exponent_for_large_numbers=True,
large_number_threshold=1e20,
significant_figures_after_decimal_point=5,
))
2.4.2. Print small numbers in exponential notation
str(ReadableNumber(0.000000012, use_exponent_for_small_numbers=True))
str(ReadableNumber(
-0.000000123456,
use_exponent_for_small_numbers=True,
precision=2,
))
str(ReadableNumber(
-0.000012345,
use_exponent_for_small_numbers=True,
precision=None,
small_number_threshold=1e-2,
))
str(ReadableNumber(
-0.00000012345,
use_exponent_for_small_numbers=True,
significant_figures_after_decimal_point=2,
))
3. Full API documentation
Please visit this site: https://readable-number.readthedocs.io/en/stable/