You are here

function dba_statpack_short in Database Administration 7

Format number.

3 calls to dba_statpack_short()
dba_mysql_statpack_format in database/mysql.statpack.inc
dba_mysql_statpack_output in database/mysql.statpack.inc
dba_mysql_statpack_statement in database/mysql.statpack.inc

File

./dba.statpack.inc, line 167

Code

function dba_statpack_short($number, $kb = FALSE, $d = 2) {
  $n = 0;
  $format = array();
  if ($kb) {
    $format = array(
      'b',
      'Kb',
      'Mb',
      'Gb',
      'Tb',
    );
    while ($number > 1023) {
      $number /= 1024;
      $n++;
    }
  }
  else {
    $format = array(
      '',
      'K',
      'M',
      'G',
      'T',
    );
    while ($number > 999) {
      $number /= 1000;
      $n++;
    }
  }
  $short = sprintf("%.{$d}f%s", $number, $format[$n]);
  if (preg_match('/^(.+)\\.00$/', $short, $matches)) {
    return $matches[1];

    // Convert 12.00 to 12, but not 12.00kb to 12kb
  }
  return $short;
}