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) |
Результат | string | the formatted value and its unit |
/** * 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) . ' ' . __('pages') . ' / ' . join(' ', 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; }
/** * Handles request for real row count on database level view page. * * @return boolean true */ public function handleRealRowCountRequestAction() { $ajax_response = $this->response; // If there is a request to update all table's row count. if (!isset($_REQUEST['real_row_count_all'])) { // Get the real row count for the table. $real_row_count = $this->dbi->getTable($this->db, $_REQUEST['table'])->getRealRowCountTable(); // Format the number. $real_row_count = Util::formatNumber($real_row_count, 0); $ajax_response->addJSON('real_row_count', $real_row_count); return; } // Array to store the results. $real_row_count_all = array(); // Iterate over each table and fetch real row count. foreach ($GLOBALS['tables'] as $table) { $row_count = $this->dbi->getTable($this->db, $table['TABLE_NAME'])->getRealRowCountTable(); $real_row_count_all[] = array('table' => $table['TABLE_NAME'], 'row_count' => $row_count); } $ajax_response->addJSON('real_row_count_all', json_encode($real_row_count_all)); }
/** * Format Variable * * @param string $name variable name * @param integer $value variable value * * @return array formatted string and bool if string is HTML formatted */ private function _formatVariable($name, $value) { $isHtmlFormatted = false; $formattedValue = $value; if (is_numeric($value)) { if (isset($this->variable_doc_links[$name][3]) && $this->variable_doc_links[$name][3] == 'byte') { $isHtmlFormatted = true; $formattedValue = '<abbr title="' . htmlspecialchars(Util::formatNumber($value, 0)) . '">' . htmlspecialchars(implode(' ', Util::formatByteDown($value, 3, 3))) . '</abbr>'; } else { $formattedValue = Util::formatNumber($value, 0); } } return array($formattedValue, $isHtmlFormatted); }
/** * Format Variable * * @param string $name variable name * @param number $value variable value * * @return string formatted string */ private function _formatVariable($name, $value) { if (is_numeric($value)) { if (isset($this->variable_doc_links[$name][3]) && $this->variable_doc_links[$name][3] == 'byte') { return '<abbr title="' . Util::formatNumber($value, 0) . '">' . implode(' ', Util::formatByteDown($value, 3, 3)) . '</abbr>'; } else { return Util::formatNumber($value, 0); } } return $value; }
/** * Returns as HTML table of the engine's server variables * * @return string The table that was generated based on the retrieved * information */ public function getHtmlVariables() { $odd_row = false; $ret = ''; foreach ($this->getVariablesStatus() as $details) { $ret .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">' . "\n" . ' <td>' . "\n"; if (!empty($details['desc'])) { $ret .= ' ' . Util::showHint($details['desc']) . "\n"; } $ret .= ' </td>' . "\n" . ' <th>' . htmlspecialchars($details['title']) . '</th>' . "\n" . ' <td class="value">'; switch ($details['type']) { case PMA_ENGINE_DETAILS_TYPE_SIZE: $parsed_size = $this->resolveTypeSize($details['value']); $ret .= $parsed_size[0] . ' ' . $parsed_size[1]; unset($parsed_size); break; case PMA_ENGINE_DETAILS_TYPE_NUMERIC: $ret .= Util::formatNumber($details['value']) . ' '; break; default: $ret .= htmlspecialchars($details['value']) . ' '; } $ret .= '</td>' . "\n" . '</tr>' . "\n"; $odd_row = !$odd_row; } if (!$ret) { $ret = '<p>' . "\n" . ' ' . __('There is no detailed status information available for this ' . 'storage engine.') . "\n" . '</p>' . "\n"; } else { $ret = '<table class="data">' . "\n" . $ret . '</table>' . "\n"; } return $ret; }