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).
MetricSystem
This is another enum used to convert byte units between different metric systems.
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
Format numbers using PHP's number_format built-in function but removing thousands separator and some other improvements:
setPrecision
See asRound to remove decimals instead of setting this option to 0.
Customise precision for some conversion operations like divisions:
asRound
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:
usingBytes
Perform conversions using bytes 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
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