Packagist GitLab GitHub Bitbucket Gitea

A metric formatter for PHP.


composer require arokettu/kilo-mega


Formatting a metric value

use function \Arokettu\KiloMega\format_metric;
echo format_metric(1000, suffix: 'W'); // 1.00 kW

Formatting a byte or bit value

use function \Arokettu\KiloMega\format_bytes;
echo format_bytes(1234); // 1.23 KiB
// equivalent to
use function \Arokettu\KiloMega as km;
echo format_metric(
    prefixes: km\SHORT_BINARY_PREFIXES, // changed default
    scale: km\SCALE_BINARY, // hardcoded
    onlyIntegers: true, // hardcoded



Using named parameters is strongly recommended. Param ordering is not guaranteed.


Unit preferences:

  • SHORT_PREFIXES. ‘k’, ‘M’, ‘G’, …

  • LONG_PREFIXES. ‘kilo’, ‘mega’, ‘giga’, …

  • SHORT_BINARY_PREFIXES (only int). ‘Ki’, ‘Mi’, ‘Gi’, …

  • LONG_BINARY_PREFIXES (only int). ‘kibi’, ‘mebi’, ‘gibi’, …

  • Custom prefixes: must be set for ranges 1-10 and, if not using onlyIntegers, also -1-10

Default: SHORT_PREFIXES for metric, SHORT_BINARY_PREFIXES for bytes


Unit name, 'B' by default


Separator string between the number and the unit, ' ' by default. Override if you want non-breaking space there or no space at all.


Unit prefix scale.

  • SCALE_METRIC = 1000

  • SCALE_BINARY = 1024

  • Custom can be used but is not supported

Default: SCALE_METRIC for metric. Hardcoded as SCALE_BINARY for bytes.


Value can only be integer, there won’t be negative scale prefixes and prefix-less scale won’t have a decimal point. Default: false for metric. Hardcoded as true for bytes.


Format type:

  • false. Optimized for variable width fonts, 3 digit precision will be used,

    with 4 possible for binary scale in range 1000-1023 (1.23, 12.3, 112, 1012).

  • true. Optimized for fixed width fonts. 3 characters will be used including decimal (1.2, 12, 123)

Default: false.


Force show + for positive values. Default: false.


The library is available as open source under the terms of the MIT License.