Skip to main content

binary

Latest PyPI version GitHub Actions Codecov Supported Python versions License GitHub sponsors

binary provides a bug-free and easy way to convert between and within binary (IEC) and decimal (SI) units.

Installation

$ pip install binary

Examples

Let’s import what we need:

>>> from binary import BinaryUnits, DecimalUnits, convert_units

Basic conversion:

>>> convert_units(1536, BinaryUnits.KB, BinaryUnits.MB)
(1.5, 'MiB')

How much actual storage your new hard drive has:

>>> convert_units(4, DecimalUnits.TB, BinaryUnits.TB)
(3.637978807091713, 'TiB')

Human readable:

>>> amount, unit = convert_units(kubernetes_ingest_bytes_per_second)
>>> 'Incoming traffic: {:.2f} {}/s'.format(amount, unit)
'Incoming traffic: 24.77 GiB/s'

Usage

Just a single function!

convert_units(n, unit=BYTE, to=None, si=False, exact=False)

Converts between and within binary and decimal units. If no unit is specified, n is assumed to already be in bytes. If no to is specified, n will be converted to the highest unit possible. If no unit nor to is specified, the output will be binary units unless si is True. If exact is True. the calculations will use decimal.Decimal.

  • Parameters

    • n (int or float) - The number of units.

    • unit - The unit n represents. See types.

    • to - The unit to convert to. See types.

    • si (bool) - Assume SI units when no unit nor to is specified.

    • exact (bool) - Use decimal.Decimal for calculations.

Types

Although the string representations for binary units end in iB, the attributes do not for ease of use.

Type

Short

Long

BinaryUnits

B

BYTE

BinaryUnits

KB

KIBIBYTE

BinaryUnits

MB

MEBIBYTE

BinaryUnits

GB

GIBIBYTE

BinaryUnits

TB

TEBIBYTE

BinaryUnits

PB

PEBIBYTE

BinaryUnits

EB

EXBIBYTE

BinaryUnits

ZB

ZEBIBYTE

BinaryUnits

YB

YOBIBYTE

DecimalUnits

B

BYTE

DecimalUnits

KB

KILOBYTE

DecimalUnits

MB

MEGABYTE

DecimalUnits

GB

GIGABYTE

DecimalUnits

TB

TERABYTE

DecimalUnits

PB

PETABYTE

DecimalUnits

EB

EXABYTE

DecimalUnits

ZB

ZETTABYTE

DecimalUnits

YB

YOTTABYTE

License

binary is distributed under the terms of both

at your option.

Changelog

Important changes are emphasized.

1.0.2

  • Perform floor division when explicitly converting to bytes

  • Support negative values

1.0.1

  • Fix syntax warning on newer versions of Python

1.0.0

  • Initial release

Metadata

Release files for binary 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binary 1.0.2
File Size Uploaded
binary-1.0.2.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for binary 1.0.2
File Interpreter ABI Platform
binary-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / binary-1.0.2.tar.gz

Download URL binary-1.0.2.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a6ba0af9579098b18dd2ec0b08bd409d8a5c4e5e5a301104b053ad40137264a8
BLAKE2b-256 checksum
How to use checksums
bed66ec8a59124533f66e8873eaf6cb2ecbc75c37629aa33c80d2d6338a9eddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 11, 2025.

Transparency log

Release files / binary-1.0.2-py3-none-any.whl

Download URL binary-1.0.2-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e79bba085cddfb9d3beefe138464506d2ef0c0f5f68c69e27fc9f82d4ac85470
BLAKE2b-256 checksum
How to use checksums
3dc4482b01ffc8a7b7a1a99497fa46d3fca315219b8ddd8f5e595044b7e0ff18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 11, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page