{"id":6546,"library":"binary","title":"Binary Unit Conversion","description":"The 'binary' library (ofek/binary) provides a straightforward and reliable way to convert between binary (IEC) and decimal (SI) units. It is currently at version 1.0.2, with releases driven by bug fixes and new features, maintaining an active development status.","status":"active","version":"1.0.2","language":"en","source_language":"en","source_url":"https://github.com/ofek/binary","tags":["unit conversion","binary units","decimal units","IEC","SI","data size"],"install":[{"cmd":"pip install binary","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"BinaryUnits","correct":"from binary import BinaryUnits"},{"symbol":"DecimalUnits","correct":"from binary import DecimalUnits"},{"symbol":"convert_units","correct":"from binary import convert_units"}],"quickstart":{"code":"from binary import BinaryUnits, DecimalUnits, convert_units\n\n# Basic conversion from KB to MB (binary units)\namount, unit = convert_units(1536, BinaryUnits.KB, BinaryUnits.MB)\nprint(f\"1536 KB is {amount} {unit}\")\n\n# How much actual storage your new hard drive has (Decimal TB to Binary TiB)\namount, unit = convert_units(4, DecimalUnits.TB, BinaryUnits.TB)\nprint(f\"4 Decimal TB is {amount:.2f} {unit}\")\n\n# Human readable output (converts to highest possible unit)\nkubernetes_ingest_bytes_per_second = 26666666666 # Example large number of bytes\namount, unit = convert_units(kubernetes_ingest_bytes_per_second)\nprint(f\"Incoming traffic: {amount:.2f} {unit}/s\")","lang":"python","description":"Demonstrates basic unit conversion, calculating actual storage from advertised decimal units, and generating human-readable unit representations."},"warnings":[{"fix":"Review any code explicitly converting units to bytes and verify that floor division provides the expected results. Adjust logic if rounding behavior has changed unexpectedly.","message":"Version 1.0.2 introduced a change where explicit conversion to bytes now performs floor division. This might alter results for some calculations compared to prior versions.","severity":"breaking","affected_versions":">=1.0.2"},{"fix":"Always explicitly specify `BinaryUnits` or `DecimalUnits` to avoid ambiguity. Refer to IEC 80000-13 and SI standards for clarity if unsure.","message":"The library differentiates between binary (IEC, e.g., KiB, MiB) and decimal (SI, e.g., KB, MB) units. Misunderstanding these standards can lead to incorrect calculations, especially when comparing advertised storage (SI) with actual usable space (IEC).","severity":"gotcha","affected_versions":"all"},{"fix":"For explicit control, always provide both the `unit` and `to` arguments. If relying on automatic conversion, be mindful of the default behavior for binary vs. SI units.","message":"When `convert_units` is called without specifying a `unit` or `to` parameter, it defaults to assuming `n` is in bytes and converts to the highest possible binary unit. If `si=True` is passed, it converts to the highest SI unit. Ensure these defaults align with your intended conversion.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}