KiloMega#
A metric formatter for PHP.
Installation#
composer require arokettu/kilo-mega
Documentation#
Formatting a metric value#
<?php
use function \Arokettu\KiloMega\format_metric;
echo format_metric(1000, suffix: 'W'); // 1.00 kW
Formatting a byte or bit value#
<?php
use function \Arokettu\KiloMega\format_bytes;
echo format_bytes(1234); // 1.23 KiB
// equivalent to
use function \Arokettu\KiloMega as km;
echo format_metric(
1234,
prefixes: km\SHORT_BINARY_PREFIXES, // changed default
scale: km\SCALE_BINARY, // hardcoded
onlyIntegers: true, // hardcoded
);
Parameters#
Warning
Using named parameters is strongly recommended. Param ordering is not guaranteed.
$prefixesUnit 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_PREFIXESfor metric,SHORT_BINARY_PREFIXESfor bytes$suffixUnit name,
'B'by default$separatorSeparator string between the number and the unit,
' 'by default. Override if you want non-breaking space there or no space at all.$scaleBaseUnit prefix scale.
SCALE_METRIC = 1000SCALE_BINARY = 1024Custom can be used but is not supported
Default:
SCALE_METRICfor metric. Hardcoded asSCALE_BINARYfor bytes.$onlyIntegersValue can only be integer, there won’t be negative scale prefixes and prefix-less scale won’t have a decimal point. Default:
falsefor metric. Hardcoded astruefor bytes.$fixedWidthFormat 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.$forceSignForce show
+for positive values. Default:false.
License#
The library is available as open source under the terms of the MIT License.