Skip to main content

Prefixed alternative numeric library

Project description

Documentation Status Travis-CI Build 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'

>>> '{:.2j}B'.format(Float(42467328))
'40.50MiB'

>>> f'{Float(2048):.2J}B'
'2.00KB'

Because prefixed.Float inherits from the built-in float, it behaves exactly the same in most cases.

Key differences:

  • 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', 'j', and 'J' are supported for f-strings and format().

    Type

    Meaning

    'h'

    SI format. Outputs the number with closest divisible SI prefix. (k, M, G, …)

    'j'

    IEC Format. Outputs the number with closest divisible IEC prefix. (Ki, Mi, Gi, …)

    'J'

    Short IEC Format. Same as 'j' but only a single character. (K, M, G, …)

  • 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'
  • 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

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

IEC (Binary) Prefixes

Prefix

Name

Base

Y

Yobi

1080

Z

Zebi

1070

E

Exbi

1060

P

Pedi

1050

T

Tebi

1040

G

Gibi

1030

M

Mebi

1020

K

Kibi

1010

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.3.1.tar.gz (43.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prefixed-0.3.1-py2.py3-none-any.whl (11.9 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: prefixed-0.3.1.tar.gz
  • Upload date:
  • Size: 43.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for prefixed-0.3.1.tar.gz
Algorithm Hash digest
SHA256 c42510ab9259c40b6a5ffc84af022c5bc70061bec298801eed88b2ff934b24d0
MD5 22934e5029569d3baf7a559d53af87b5
BLAKE2b-256 8a63a8fc8b5fe239eaf4af875c2a3b4fbfc4c2b16644ab8ae879d58351aadf84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prefixed-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for prefixed-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 78391d02ba3f385ec2517ccd29c578413d7f0d2a69799d3e9f9a833368d26c35
MD5 e3abf9eaa0b75955e8bab4b8a30c2558
BLAKE2b-256 34fa9e553176677506206530eea9ba0feb140dbb278d90492fde3862f2fd3c0e

See more details on using hashes here.

Supported by

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