Usage
Full documentation on how to use the ByteUnitConverter library on your PHP project.
All methods use numeric strings as arguments and returns numeric strings as the results. All this is because native PHP integers or floats doesn't support big numbers. That's why this library also requires BCMath extension.
ByteUnit
This enum works as a type safety for conversions using ByteUnitConverter
utility class, check its documentation to see its usage.
DataUnit
Same as ByteUnit, this is also an enum used for perform conversion between different data units (bytes or bits).
Check this Wikipedia article to understand what a byte is in terms of bytes and more.
MetricSystem
This is another enum used to convert byte units between different metric systems.
Check this Wikipedia article to understand what a metric system means as of a general perspective.
ByteUnitConverter
This utility class is used to convert between:
Different byte units (KB to MB, TB to GB, etc...)
Different data units (kilobytes to kilobits, bytes to bits, etc...)
And different metric systems (kilobytes to kibibytes, tebibytes to gigabytes, etc...).
new
Create new instance from bytes:
toBytesFromUnit
Get bytes from unit:
from
Get new instance from value and unit:
numberFormat
Reused internally within the library but publicly available.
Format numbers using PHP's number_format
built-in function but removing thousands separator and some other improvements:
setPrecision
Default to 2 as is same as the decimal positions from the output.
See asRound to remove decimals instead of setting this option to 0.
Customise precision for some conversion operations like divisions:
asRound
As for v3 this is now accepting integers and booleans as input argument. Defaults to at least 2 decimals if possible.
Round result to a integer with as less decimals as possible:
Disable any rounding on the result:
Round as much as possible until reach 3 decimals:
useUnitLabel
Round result to a integer without decimals:
nearestUnit
Convert bytes to their nearest unit on the specified metric system:
getValue
Get the resulting numeric value from the conversion:
getUnit
Get the resulting byte unit as string from the conversion:
to
Convert bytes to byte unit:
All byte units from all metric systems available in this library have their own conversion methods for convenience.
usingBytes
Perform conversions using bytes data unit:
This library already use bytes as a default data unit.
usingBits
Perform conversions using bits data unit:
__toString
Serialise conversion result to string:
toArray
Serialise conversion result to multidimensional array:
serialize
The ByteUnitConverter
utility class can be also serialised/deserialised using PHP serialisation:
add
Manipulation methods returns new instances of the ByteUnitConverter
to remain immutable.
Add quantity of any byte unit to the current instance making a new one:
subtract
Remove quantity of any byte unit to the current instance making a new one:
Last updated