format_size($size, $langcode = NULL)
drupal/includes/common.inc, line 1436
Generate a string representation for the given byte count.
$size A size in bytes.
$langcode Optional language code to translate to a language other than what is used to display the page.
A translated string representation of the size.
| Name | Description |
|---|---|
| Formatting | Functions to format numbers, strings, dates, etc. |
| Input validation | Functions to validate user input. |
function format_size($size, $langcode = NULL) {
if ($size < DRUPAL_KILOBYTE) {
return format_plural($size, '1 byte', '@count bytes', array(), $langcode);
}
else {
$size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes.
$units = array(
t('@size KB', array(), $langcode),
t('@size MB', array(), $langcode),
t('@size GB', array(), $langcode),
t('@size TB', array(), $langcode),
t('@size PB', array(), $langcode),
t('@size EB', array(), $langcode),
t('@size ZB', array(), $langcode),
t('@size YB', array(), $langcode),
);
foreach ($units as $unit) {
if (round($size, 2) >= DRUPAL_KILOBYTE) {
$size = $size / DRUPAL_KILOBYTE;
}
else {
break;
}
}
return str_replace('@size', round($size, 2), $unit);
}
}