Source for file number.php

Documentation is available at number.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  HTML
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * HTML helper class for rendering numbers.
  14.  *
  15.  * @package     Joomla.Libraries
  16.  * @subpackage  HTML
  17.  * @since       1.6
  18.  */
  19. abstract class JHtmlNumber
  20. {
  21.     /**
  22.      * Converts bytes to more distinguishable formats such as:
  23.      * kilobytes, megabytes, etc.
  24.      *
  25.      * By default, the proper format will automatically be chosen.
  26.      * However, one of the allowed unit types may also be used instead.
  27.      *
  28.      * @param   integer  $bytes      The number of bytes.
  29.      * @param   string   $unit       The type of unit to return.
  30.      * @param   integer  $precision  The number of digits to be used after the decimal place.
  31.      *
  32.      * @return  string   The number of bytes in the proper units.
  33.      *
  34.      * @since   1.6
  35.      */
  36.     public static function bytes($bytes$unit 'auto'$precision 2)
  37.     {
  38.         // No explicit casting $bytes to integer here, since it might overflow
  39.         // on 32-bit systems
  40.         $precision = (int) $precision;
  41.  
  42.         if (empty($bytes))
  43.         {
  44.             return 0;
  45.         }
  46.  
  47.         $unitTypes array('b''kb''MB''GB''TB''PB');
  48.  
  49.         // Default automatic method.
  50.         $i floor(log($bytes1024));
  51.  
  52.         // User supplied method:
  53.         if ($unit !== 'auto' && in_array($unit$unitTypes))
  54.         {
  55.             $i array_search($unit$unitTypestrue);
  56.         }
  57.  
  58.         // TODO Allow conversion of units where $bytes = '32M'.
  59.  
  60.         return round($bytes pow(1024$i)$precision' ' $unitTypes[$i];
  61.     }
  62. }

Documentation generated on Tue, 19 Nov 2013 15:09:39 +0100 by phpDocumentor 1.4.3