{"id":13623846,"url":"https://github.com/gabrielelana/byte-units","last_synced_at":"2025-04-04T19:14:16.612Z","repository":{"id":17827161,"uuid":"20730294","full_name":"gabrielelana/byte-units","owner":"gabrielelana","description":"Library to parse, format and convert byte units","archived":false,"fork":false,"pushed_at":"2021-01-16T23:46:30.000Z","size":61,"stargazers_count":160,"open_issues_count":6,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-05-17T04:01:46.591Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gabrielelana.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-11T15:22:54.000Z","updated_at":"2023-11-11T05:17:34.000Z","dependencies_parsed_at":"2022-08-07T09:00:18.741Z","dependency_job_id":null,"html_url":"https://github.com/gabrielelana/byte-units","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielelana%2Fbyte-units","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielelana%2Fbyte-units/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielelana%2Fbyte-units/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielelana%2Fbyte-units/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gabrielelana","download_url":"https://codeload.github.com/gabrielelana/byte-units/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234923,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T21:01:36.339Z","updated_at":"2025-04-04T19:14:16.583Z","avatar_url":"https://github.com/gabrielelana.png","language":"PHP","funding_links":[],"categories":["数字","目录","Table of Contents","PHP","数字 Numbers","Text and Numbers","Numbers","数字( Numbers )"],"sub_categories":["数字 Numbers","Numbers","Library"],"readme":"## Byte Units [![Build Status](https://travis-ci.org/gabrielelana/byte-units.svg?branch=master)](https://travis-ci.org/gabrielelana/byte-units)\nThis is a utility component for parsing, formatting, converting and manipulating byte units in various formats.\n\n## Usage\n```php\n\u003c?php\n\n// Bytes manipulation and formatting with explici precision\necho ByteUnits\\parse('1.42MB')-\u003eadd('256B')-\u003eformat('kB/0000'); // outputs 1420.2560kB\n\n// Bytes comparison\nByteUnits\\parse('1.2GB')-\u003eisMoreThan('256MB'); // it's true\n```\n\n### Parsing\nRight now two unit systems are supported:\n* The `Metric` system that is based on a 1000-byte kilobyte and uses standard SI suffixes (`kB`, `MB`, `GB`, `TB`, `PB`, …)\n* The `Binary` system that is based on a 1024-byte kilobyte and uses binary suffixes (`KiB`, `MiB`, `GiB`, `TiB`, `PiB`, …)\n\n```php\n\u003c?php\n\n// Explicit system selection\necho ByteUnits\\Metric::bytes(1000)-\u003eformat();  // outputs 1.00kB\necho ByteUnits\\Binary::bytes(1024)-\u003eformat();  // outputs 1.00KiB\n\n// Implicit selection through parsing\nByteUnits\\parse('1.00kB'); // it's an instance of ByteUnits\\Metric\n\n// You can also constraint parsing to a specific system\nByteUnits\\Metric::parse('1.00kB'); // it's an instance of ByteUnits\\Metric\nByteUnits\\Binary::parse('1.00kB'); // throws a ByteUnits\\ParseException\n\n// For each systems there are static constructors, one for each supported unit\necho ByteUnits\\Metric::bytes(1000)-\u003eformat();  // outputs 1.00kB\necho ByteUnits\\Metric::kilobytes(1)-\u003eformat();  // outputs 1.00kB\necho ByteUnits\\Metric::megabytes(1)-\u003eformat();  // outputs 1.00MB\n\n// You can switch between systems\necho ByteUnits\\Binary::bytes(1024)-\u003easMetric()-\u003eformat(); // outputs 1.02kB\n```\n\n### Formatting\nIn both systems you can format bytes with an appropriate format string\n```php\n\u003c?php\n\n// By defaults it tries to format bytes in the most readable unit\necho ByteUnits\\bytes(1322000)-\u003eformat(); // outputs 1.32MB\necho ByteUnits\\bytes(132200)-\u003eformat(); // outputs 132.20kB\n\n// You can force the unit using the related suffix\necho ByteUnits\\bytes(1322000)-\u003eformat('MB'); // outputs 1.32MB\necho ByteUnits\\bytes(1322000)-\u003eformat('kB'); // outputs 1322.00kB\necho ByteUnits\\bytes(1322000)-\u003eformat('B'); // outputs 1322000B\n\n// You can choose the precision aka the number of digits after the `.`\necho ByteUnits\\bytes(1322123)-\u003eformat(6); // outputs 1.322123MB\necho ByteUnits\\bytes(1322123)-\u003eformat('/6'); // outputs 1.322123MB\necho ByteUnits\\bytes(1322123)-\u003eformat('MB/6'); // outputs 1.322123MB\necho ByteUnits\\bytes(1322123)-\u003eformat('MB/000000'); // outputs 1.322123MB\necho ByteUnits\\bytes(1322123)-\u003eformat('GB/9'); // outputs 0.001322123GB\n\n// You can specify a separator between then number and the units\necho ByteUnits\\bytes(1322000)-\u003eformat('MB', ' '); // outputs 1.32 MB\necho ByteUnits\\bytes(1322000)-\u003eformat('MB', '/'); // outputs 1.32/MB\n\n// If you don't want to format but get the number of bytes\n// NOTE: The output is a string to ensure that there's no overflow\necho ByteUnits\\bytes(1322000)-\u003enumberOfBytes(); // outputs 1322000\n```\n\n### Compare\nThere are a few methods that could be used to compare bytes in various units and systems\n```php\n\u003c?php\n\nByteUnits\\Metric::kilobytes(1)-\u003eisLessThan(ByteUnits\\Binary::kilobytes(1)); // it's true\nByteUnits\\Metric::kilobytes(1)-\u003eisEqualTo(ByteUnits\\Binary::bytes(1000)); // it's true\nByteUnits\\Metric::kilobytes(1.3)-\u003eisGreaterThan(ByteUnits\\Binary::kilobytes(1)); // it's true\n```\n\n### Manipulate\nAlso you can add or remove bytes in various units and systems\n```php\n\u003c?php\n\necho ByteUnits\\Binary::kilobytes(1)-\u003eremove(ByteUnits\\Metric::kilobytes(1))-\u003eformat(); // outputs 24B\n\n// Arithmetic operations always preserves the receiving unit system\necho ByteUnits\\Binary::kilobytes(1)-\u003eadd(ByteUnits\\Metric::kilobytes(1))-\u003eformat(); // outputs 1.98KiB\n\n// You cannot have negative bytes\nByteUnits\\Metric::kilobytes(1)-\u003eremove(ByteUnits\\Binary::kilobytes(1))-\u003eformat(); // throws ByteUnits\\NegativeBytesException\n```\n\n### Auto Boxing\nMost of the methods can take integers or strings and box them to appropriate byte units\n```php\n\nByteUnits\\Metric::kilobytes(1)-\u003eisLessThan('1KiB'); // it's true\necho ByteUnits\\Binary::kilobytes(1)-\u003eremove('1KiB')-\u003eformat(); // outputs 24B\n```\n\n## Installation via [Composer](http://getcomposer.org/)\n\n* Install Composer to your project root:\n  ```bash\n  curl -sS https://getcomposer.org/installer | php\n  ```\n\n* Add a `composer.json` file to your project:\n  ```json\n  {\n    \"require\": {\n      \"gabrielelana/byte-units\": \"^0.5\"\n    }\n  }\n  ```\n\n* Run the Composer installer:\n  ```bash\n  php composer.phar install\n  ```\n\n## Self-Promotion\nIf you like this project, then consider to:\n* Star the repository on [GitHub](https://github.com/gabrielelana/byte-units)\n* Follow me on\n  * [Twitter](http://twitter.com/gabrielelana)\n  * [GitHub](https://github.com/gabrielelana)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabrielelana%2Fbyte-units","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgabrielelana%2Fbyte-units","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabrielelana%2Fbyte-units/lists"}