Skip to main content

Prefixed alternative numeric library

Project description

Documentation Status GitHub Actions Status Coverage Status
PyPI Package latest release Supported versions Supported implementations

Overview

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.

Presentation Types

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

String Initialization

When initializing from strings, SI and IEC prefixes are honored

>>> Float('2k')
Float(2000.0)

>>> Float('2Ki')
Float(2048.0)

Additional Flags

An additional format flag ‘!’ is available which adds a space before the prefix

>>> f'{Float(3250):!.2h}'
'3.25 k'

Significant Digits

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'

Adjustable Thresholds

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'

Supported Prefixes

SI (Decimal) Prefixes

Prefix

Name

Base

Q

Quetta

1030

R

Ronna

1027

Y

Yotta

1024

Z

Zetta

1021

E

Exa

1018

P

Peta

1015

T

Tera

1012

G

Giga

109

M

Mega

106

k

Kilo

103

m

Milli

10-3

μ

Micro

10-6

n

Nano

10-9

p

Pico

10-12

f

Femto

10-15

a

Atto

10-18

z

Zepto

10-21

y

Yocto

10-24

r

Ronto

10-27

q

Quecto

10-30

IEC (Binary) Prefixes

Prefix

Name

Base

Y

Yobi

280

Z

Zebi

270

E

Exbi

260

P

Pedi

250

T

Tebi

240

G

Gibi

230

M

Mebi

220

K

Kibi

210

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

prefixed-0.9.0.tar.gz (99.3 kB view details)

Uploaded Source

Built Distribution

prefixed-0.9.0-py2.py3-none-any.whl (13.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file prefixed-0.9.0.tar.gz.

File metadata

  • Download URL: prefixed-0.9.0.tar.gz
  • Upload date:
  • Size: 99.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.4

File hashes

Hashes for prefixed-0.9.0.tar.gz
Algorithm Hash digest
SHA256 164403fa9ebc83280bbc4705f4b243a28837e164310b4e65c38ccab1ebafeeb3
MD5 a1fbee3ee3ee7b271a627f0b1043afa2
BLAKE2b-256 48ae296470eca349da35bdec573e54320afa2b5b099c582e5d97be8bccac247e

See more details on using hashes here.

File details

Details for the file prefixed-0.9.0-py2.py3-none-any.whl.

File metadata

  • Download URL: prefixed-0.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.4

File hashes

Hashes for prefixed-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3cdb74bfc4cf0aba28f3574662b13afdcac27c463dcbef320fe5d03f4c5fbca8
MD5 984683e61a4298b38c99c71cd1dfc1c7
BLAKE2b-256 c361d9957a0ed189ac8a89075c59c97588fb1c77e51d8d44a26f5cdf001d260c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page