PMA\libraries\Util::formatNumber PHP Method

formatNumber() public static method

examples: echo formatNumber(123456789, 6); // 123,457 k echo formatNumber(-123456789, 4, 2); // -123.46 M echo formatNumber(-0.003, 6); // -3 m echo formatNumber(0.003, 3, 3); // 0.003 echo formatNumber(0.00003, 3, 2); // 0.03 m echo formatNumber(0, 6); // 0
public static formatNumber ( double $value, integer $digits_left = 3, integer $digits_right, boolean $only_down = false, boolean $noTrailingZero = true ) : string
$value double the value to format
$digits_left integer number of digits left of the comma
$digits_right integer number of digits right of the comma
$only_down boolean do not reformat numbers below 1
$noTrailingZero boolean removes trailing zeros right of the comma (default: true)
return string the formatted value and its unit
    public static function formatNumber($value, $digits_left = 3, $digits_right = 0, $only_down = false, $noTrailingZero = true)
    {
        if ($value == 0) {
            return '0';
        }
        $originalValue = $value;
        //number_format is not multibyte safe, str_replace is safe
        if ($digits_left === 0) {
            $value = number_format($value, $digits_right, __('.'), __(','));
            if ($originalValue != 0 && floatval($value) == 0) {
                $value = ' <' . 1 / pow(10, $digits_right);
            }
            return $value;
        }
        // this units needs no translation, ISO
        $units = array(-8 => 'y', -7 => 'z', -6 => 'a', -5 => 'f', -4 => 'p', -3 => 'n', -2 => '&micro;', -1 => 'm', 0 => ' ', 1 => 'k', 2 => 'M', 3 => 'G', 4 => 'T', 5 => 'P', 6 => 'E', 7 => 'Z', 8 => 'Y');
        // check for negative value to retain sign
        if ($value < 0) {
            $sign = '-';
            $value = abs($value);
        } else {
            $sign = '';
        }
        $dh = pow(10, $digits_right);
        /*
         * This gives us the right SI prefix already,
         * but $digits_left parameter not incorporated
         */
        $d = floor(log10($value) / 3);
        /*
         * Lowering the SI prefix by 1 gives us an additional 3 zeros
         * So if we have 3,6,9,12.. free digits ($digits_left - $cur_digits)
         * to use, then lower the SI prefix
         */
        $cur_digits = floor(log10($value / pow(1000, $d)) + 1);
        if ($digits_left > $cur_digits) {
            $d -= floor(($digits_left - $cur_digits) / 3);
        }
        if ($d < 0 && $only_down) {
            $d = 0;
        }
        $value = round($value / (pow(1000, $d) / $dh)) / $dh;
        $unit = $units[$d];
        // number_format is not multibyte safe, str_replace is safe
        $formattedValue = number_format($value, $digits_right, __('.'), __(','));
        // If we don't want any zeros, remove them now
        if ($noTrailingZero && strpos($formattedValue, '.') !== false) {
            $formattedValue = preg_replace('/\\.?0+$/', '', $formattedValue);
        }
        if ($originalValue != 0 && floatval($value) == 0) {
            return ' <' . number_format(1 / pow(10, $digits_right), $digits_right, __('.'), __(',')) . ' ' . $unit;
        }
        return $sign . $formattedValue . ' ' . $unit;
    }

Usage Example

 /**
  * returns html tables with stats over inno db buffer pool
  *
  * @return string  html table with stats
  */
 public function getPageBufferpool()
 {
     // The following query is only possible because we know
     // that we are on MySQL 5 here (checked above)!
     // side note: I love MySQL 5 for this. :-)
     $sql = '
          SHOW STATUS
         WHERE Variable_name LIKE \'Innodb\\_buffer\\_pool\\_%\'
            OR Variable_name = \'Innodb_page_size\';';
     $status = $GLOBALS['dbi']->fetchResult($sql, 0, 1);
     $output = '<table class="data" id="table_innodb_bufferpool_usage">' . "\n" . '    <caption class="tblHeaders">' . "\n" . '        ' . __('Buffer Pool Usage') . "\n" . '    </caption>' . "\n" . '    <tfoot>' . "\n" . '        <tr>' . "\n" . '            <th colspan="2">' . "\n" . '                ' . __('Total') . "\n" . '                : ' . Util::formatNumber($status['Innodb_buffer_pool_pages_total'], 0) . '&nbsp;' . __('pages') . ' / ' . join('&nbsp;', Util::formatByteDown($status['Innodb_buffer_pool_pages_total'] * $status['Innodb_page_size'])) . "\n" . '            </th>' . "\n" . '        </tr>' . "\n" . '    </tfoot>' . "\n" . '    <tbody>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Free pages') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_free'], 0) . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="even">' . "\n" . '            <th>' . __('Dirty pages') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_dirty'], 0) . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Pages containing data') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_data'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="even">' . "\n" . '            <th>' . __('Pages to be flushed') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_flushed'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Busy pages') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_misc'], 0) . "\n" . '</td>' . "\n" . '        </tr>';
     // not present at least since MySQL 5.1.40
     if (isset($status['Innodb_buffer_pool_pages_latched'])) {
         $output .= '        <tr class="even">' . '            <th>' . __('Latched pages') . '</th>' . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_pages_latched'], 0) . '</td>' . '        </tr>';
     }
     $output .= '    </tbody>' . "\n" . '</table>' . "\n\n" . '<table class="data" id="table_innodb_bufferpool_activity">' . "\n" . '    <caption class="tblHeaders">' . "\n" . '        ' . __('Buffer Pool Activity') . "\n" . '    </caption>' . "\n" . '    <tbody>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Read requests') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_read_requests'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="even">' . "\n" . '            <th>' . __('Write requests') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_write_requests'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Read misses') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_reads'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="even">' . "\n" . '            <th>' . __('Write waits') . '</th>' . "\n" . '            <td class="value">' . Util::formatNumber($status['Innodb_buffer_pool_wait_free'], 0) . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="odd">' . "\n" . '            <th>' . __('Read misses in %') . '</th>' . "\n" . '            <td class="value">' . ($status['Innodb_buffer_pool_read_requests'] == 0 ? '---' : htmlspecialchars(Util::formatNumber($status['Innodb_buffer_pool_reads'] * 100 / $status['Innodb_buffer_pool_read_requests'], 3, 2)) . ' %') . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '        <tr class="even">' . "\n" . '            <th>' . __('Write waits in %') . '</th>' . "\n" . '            <td class="value">' . ($status['Innodb_buffer_pool_write_requests'] == 0 ? '---' : htmlspecialchars(Util::formatNumber($status['Innodb_buffer_pool_wait_free'] * 100 / $status['Innodb_buffer_pool_write_requests'], 3, 2)) . ' %') . "\n" . '</td>' . "\n" . '        </tr>' . "\n" . '    </tbody>' . "\n" . '</table>' . "\n";
     return $output;
 }
All Usage Examples Of PMA\libraries\Util::formatNumber
Util