Prefixed provides an alternative implementation of the built-in float which supports formatted output with SI (decimal) and IEC (binary) prefixes.
>>> from prefixed import Float
>>> f'{Float(3250):.2h}'
'3.25k'
>>> '{:.2h}s'.format(Float(.00001534))
'15.34μs'
>>> '{:.2k}B'.format(Float(42467328))
'40.50MiB'
>>> f'{Float(2048):.2m}B'
'2.00KB'
Because prefixed.Float inherits from the built-in float, it behaves exactly the same in most cases.
When a math operation is performed with another real number type (float, int), the result will be a prefixed.Float instance.
Additional presentation types 'h'
, 'H'
, 'k'
, 'K'
, 'm'
, and 'M'
are supported for f-strings and format().
Type | Meaning |
---|---|
'h' |
SI format. Outputs the number with closest divisible SI prefix. (k, M, G, ...) |
'H' |
Same as 'h' with precision indicating significant digits. |
'k' |
IEC Format. Outputs the number with closest divisible IEC prefix. (Ki, Mi, Gi, ...) |
'K' |
Same as 'k' with precision indicating significant digits. |
'm' |
Short IEC Format. Same as 'k' but only a single character. (K, M, G, ...) |
'M' |
Same as 'm' with precision indicating significant digits. |
'j' |
Alias for 'k' - DEPRECATED |
'J' |
Alias for 'm' - DEPRECATED |
When initializing from strings, SI and IEC prefixes are honored
>>> Float('2k')
Float(2000.0)
>>> Float('2Ki')
Float(2048.0)
An additional format flag '!' is available which adds a space before the prefix
>>> f'{Float(3250):!.2h}'
'3.25 k'
When the 'H'
, 'K
, or 'M'
presentation types are used, precision is treated as the number of significant digits to include. Standard rounding will occur for the final digit.
>>> f'{Float(1246):.3h}'
'1.246k'
>>> f'{Float(1246):.3H}'
'1.25k'
By default, trailing zeros are removed.
>>> f'{Float(1000):.3H}'
'1k'
To preserve trailing zeros, include the '#'
flag.
>>> f'{Float(1000):#.3H}'
'1.00k'
An additional field, margin, can be specified which lowers or raises the threshold for for each prefix by the given percentage. Margin is specified before precision with the syntax %[-]digit+
.
>>> f'{Float(950):.2h}'
'950.00'
>>> f'{Float(950):%-5.2h}'
'0.95k'
>>> f'{Float(1000):%5.2h}'
'1000.00'
>>> f'{Float(1050):%5.2h}'
'1.05k'
Prefix | Name | Base |
---|---|---|
|
Quetta | 1030\ |
|
Ronna | 1027\ |
|
Yotta | 1024\ |
|
Zetta | 1021\ |
|
Exa | 1018\ |
|
Peta | 1015\ |
|
Tera | 1012\ |
|
Giga | 109\ |
|
Mega | 106\ |
|
Kilo | 103\ |
|
Milli | 10-3\ |
|
Micro | 10-6\ |
|
Nano | 10-9\ |
|
Pico | 10-12\ |
|
Femto | 10-15\ |
|
Atto | 10-18\ |
|
Zepto | 10-21\ |
|
Yocto | 10-24\ |
|
Ronto | 10-27\ |
|
Quecto | 10-30\ |
Prefix | Name | Base |
---|---|---|
|
Yobi | 280\ |
|
Zebi | 270\ |
|
Exbi | 260\ |
|
Pedi | 250\ |
|
Tebi | 240\ |
|
Gibi | 230\ |
|
Mebi | 220\ |
|
Kibi | 210\ |