Prefixed alternative numeric library
Prefixed provides an alternative implementation of the built-in `float` which supports formatted output with SI (decimal) and IEC (binary) prefixes. The library is actively maintained with regular updates addressing bug fixes, new SI prefixes, and improvements to string parsing and formatting behavior.
Warnings
- breaking The behavior of the `!` format flag changed in version 0.6.0. Previously, `!` would drop the space after the number if no prefix was added. It now consistently adds a space after the number, even if no prefix is present. A new `!!` flag was introduced for the old behavior (dropping the space if no prefix is added).
- deprecated The format specifiers 'j' (IEC) and 'J' (IEC short) were deprecated in version 0.4.0. They have been replaced by 'k' and 'm' respectively. While 'j' and 'J' still work, their use is discouraged and may be removed in future versions.
- gotcha Input string parsing became stricter in version 0.3.2. It no longer accepts arbitrary characters after the prefix and now specifically allows a single space between the value and the prefix. If your application relied on less strict parsing for `Float` initialization from strings, it might break.
- gotcha Prior to version 0.8.0, rounding was not correctly accounted for when determining the appropriate prefix, leading to inaccurate results (e.g., `9.999999999e-10` formatting as `1000p` instead of `1n`). This was corrected in 0.8.0.
- gotcha As of version 0.9.0, the micro symbol ('µ', U+00B5) is accepted as a valid prefix when creating `prefixed.Float` objects from strings. However, the output will still consistently use the lowercase Greek letter mu ('μ', U+03BC).
Install
-
pip install prefixed
Imports
- Float
from prefixed import Float
Quickstart
from prefixed import Float
# Basic SI formatting
value_si = Float(3250)
print(f'{value_si:.2h}')
# IEC formatting with bytes unit
value_iec = Float(42467328)
print(f'{value_iec:.2k}B')
# Significant digits with SI
value_sig_dig = Float(1246)
print(f'{value_sig_dig:.3H}')
# Initializing from strings with prefixes
from_string_k = Float('2k')
from_string_Ki = Float('2Ki')
print(f'{from_string_k=}')
print(f'{from_string_Ki=}')