Make Language::formatNum() handle TB through YB
Rewrote code to be simpler and less indenty
Though, something like formatBitrate might be be better in future... We'll see!
/**
* Format a size in bytes for output, using an appropriate
/**
* Format a size in bytes for output, using an appropriate
- * unit (B, KB, MB or GB) according to the magnitude in question
+ * unit (B, KB, MB, GB, TB, PB, EB, ZB or YB) according to the magnitude in question
*
* @param $size int Size to format
* @return string Plain text (not HTML)
*/
function formatSize( $size ) {
*
* @param $size int Size to format
* @return string Plain text (not HTML)
*/
function formatSize( $size ) {
+ $sizes = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
+ $index = 0;
+
+ $maxIndex = count( $sizes ) - 1;
+ while ( $size >= 1024 && $index < $maxIndex ) {
+ $index++;
+ $size /= 1024;
+ }
+
// For small sizes no decimal places necessary
$round = 0;
// For small sizes no decimal places necessary
$round = 0;
- if ( $size > 1024 ) {
- $size = $size / 1024;
- if ( $size > 1024 ) {
- $size = $size / 1024;
- // For MB and bigger two decimal places are smarter
- $round = 2;
- if ( $size > 1024 ) {
- $size = $size / 1024;
- $msg = 'size-gigabytes';
- } else {
- $msg = 'size-megabytes';
- }
- } else {
- $msg = 'size-kilobytes';
- }
- } else {
- $msg = 'size-bytes';
+ if ( $index > 1 ) {
+ // For MB and bigger two decimal places are smarter
+ $round = 2;
+ $msg = "size-{$sizes[$index]}bytes";
+
$size = round( $size, $round );
$text = $this->getMessageFromDB( $msg );
return str_replace( '$1', $this->formatNum( $size ), $text );
$size = round( $size, $round );
$text = $this->getMessageFromDB( $msg );
return str_replace( '$1', $this->formatNum( $size ), $text );
- "1,024 B",
- "1,024 bytes"
- ),
- array(
- 1024 + 1,
"1 KB",
"1 kilobyte"
),
array(
1024 * 1024,
"1 KB",
"1 kilobyte"
),
array(
1024 * 1024,
- "1,024 KB",
- "1,024 kilobyte"
- ),
- array(
- ( 1024 * 1024 ) + 1,
),
array(
1024 * 1024 * 1024,
),
array(
1024 * 1024 * 1024,
- "1,024 MB",
- "1,024 megabyte"
+ "1 GB",
+ "1 gigabytes"
- ( 1024 * 1024 * 1024 ) + 1,
- "1 GB",
- "1 gigabyte"
+ pow( 1024, 4 ),
+ "1 TB",
+ "1 terabyte"
+ ),
+ array(
+ pow( 1024, 5 ),
+ "1 PB",
+ "1 petabyte"
+ ),
+ array(
+ pow( 1024, 6 ),
+ "1 EB",
+ "1,024 exabyte"
+ ),
+ array(
+ pow( 1024, 7 ),
+ "1 ZB",
+ "1 zetabyte"
- ( 1024 * 1024 * 1024 * 1024 ) + 1,
- "1,024 GB",
- "1,024 gigabyte"
+ pow( 1024, 8 ),
+ "1 YB",
+ "1 yottabyte"