JBZoo\Utils\FS::format PHP Method

format() public static method

Nice formatting for computer sizes (Bytes).
public static format ( integer $bytes, integer $decimals = 2 ) : string
$bytes integer The number in bytes to format
$decimals integer The number of decimal points to include
return string
    public static function format($bytes, $decimals = 2)
    {
        $exp = 0;
        $value = 0;
        $symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
        $bytes = floatval($bytes);
        if ($bytes > 0) {
            $exp = floor(log($bytes) / log(1024));
            $value = $bytes / pow(1024, floor($exp));
        }
        if ($symbol[$exp] === 'B') {
            $decimals = 0;
        }
        return number_format($value, $decimals, '.', '') . ' ' . $symbol[$exp];
    }

Usage Example

Beispiel #1
0
 /**
  * @param int $count
  * @return array
  */
 public function runTest($count = 1)
 {
     gc_collect_cycles();
     // Forces collection of any existing garbage cycles
     $this->_profiler->start();
     for ($i = 0; $i < $count; $i++) {
         // Store the result so it appears in memory profiling
         $this->_executeTest();
     }
     $this->_profiler->stop();
     $time = $this->_profiler->getTime();
     $timeOne = $this->_profiler->getTime() / $count;
     $memory = $this->_profiler->getMemory();
     return array('time' => $time, 'time_one' => $timeOne, 'memory' => $memory, 'count' => $count, 'formated' => sprintf("Time: %s/%s; Memory: %s; Count: %s", Timer::formatMS($time), Timer::formatMS($timeOne), FS::format($memory), $count));
 }
All Usage Examples Of JBZoo\Utils\FS::format