Make WordPress Core

Ticket #5246: size_format.diff

File size_format.diff, 1.5 KB (added by nbachiyski, 18 years ago)
  • wp-includes/functions.php

     
    8989        return number_format( $number, $decimals, $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] );
    9090}
    9191
    92 
     92/**
     93 * Formats a size with the correct byte suffix.
     94 *
     95 * @param number $bytes number of bytes to format
     96 * @param int $decimals number of digits after the decimal point to be used, optional
     97 * @return string|bool formatted number (according to current locale) with the correct suffix (KB, MB, etc.) in the end or false on error
     98 */
    9399function size_format( $bytes, $decimals = null ) {
     100        if (!is_numeric($bytes)) {
     101                return false;
     102        }
    94103        // technically the correct unit names for powers of 1024 are KiB, MiB etc
    95104        // see http://en.wikipedia.org/wiki/Byte
    96105        $quant = array(
     
    98107                'TB' => 1099511627776,  // pow( 1024, 4)
    99108                'GB' => 1073741824,     // pow( 1024, 3)
    100109                'MB' => 1048576,        // pow( 1024, 2)
    101                 'kB' => 1024,           // pow( 1024, 1)
     110                'KB' => 1024,           // pow( 1024, 1)
    102111                'B ' => 1,              // pow( 1024, 0)
    103112        );
    104113
    105114        foreach ( $quant as $unit => $mag )
    106                 if ( intval( $bytes ) >= $mag )
     115                if ( $bytes >= $mag )
    107116                        return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;
     117        return false;
    108118}
    109119
    110120
     
    14841494        return abs( intval( $maybeint ) );
    14851495}
    14861496
    1487 ?>
    1488  No newline at end of file
     1497?>