Convert a numerical value to the readable string notated equivalent.
<?php
/**
* Formats a number with a level of precision (even if it had none).
*
* @param float $number
* @param int $precision
* @return float
*/
function precision($number, $precision = 2)
{
return sprintf('.' . $precision . 'F', $number);
}
/**
* Convert a numerical value to the readable string notated equivalent.
*
* @param int $size
* @param int $precision
* @return string
*/
function bytesTo($size, $precision = 0)
{
$sizes = array(
'YB',
'ZB',
'EB',
'PB',
'TB',
'GB',
'MB',
'KB',
'B'
);
$total = count($sizes);
while ($total -- && $size >= 1024)
{
$size /= 1024;
}
return precision($size, $precision) . $sizes[$total];
}