blob: 1fdd303269afcd73241aaeb4627391c7aeecbb12 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Number Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/number_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Formats a numbers as bytes, based on size, and adds the appropriate suffix
32 *
33 * @access public
34 * @param mixed // will be cast as int
35 * @return string
36 */
37if ( ! function_exists('byte_format'))
38{
Derek Allardfcbb0262010-01-17 16:13:15 +000039 function byte_format($num, $precision = 1)
Derek Allard2067d1a2008-11-13 22:59:24 +000040 {
41 $CI =& get_instance();
42 $CI->lang->load('number');
43
44 if ($num >= 1000000000000)
45 {
Derek Allardfcbb0262010-01-17 16:13:15 +000046 $num = round($num / 1099511627776, $precision);
Derek Allard2067d1a2008-11-13 22:59:24 +000047 $unit = $CI->lang->line('terabyte_abbr');
48 }
49 elseif ($num >= 1000000000)
50 {
Derek Allardfcbb0262010-01-17 16:13:15 +000051 $num = round($num / 1073741824, $precision);
Derek Allard2067d1a2008-11-13 22:59:24 +000052 $unit = $CI->lang->line('gigabyte_abbr');
53 }
54 elseif ($num >= 1000000)
55 {
Derek Allardfcbb0262010-01-17 16:13:15 +000056 $num = round($num / 1048576, $precision);
Derek Allard2067d1a2008-11-13 22:59:24 +000057 $unit = $CI->lang->line('megabyte_abbr');
58 }
59 elseif ($num >= 1000)
60 {
Derek Allardfcbb0262010-01-17 16:13:15 +000061 $num = round($num / 1024, $precision);
Derek Allard2067d1a2008-11-13 22:59:24 +000062 $unit = $CI->lang->line('kilobyte_abbr');
63 }
64 else
65 {
66 $unit = $CI->lang->line('bytes');
67 return number_format($num).' '.$unit;
68 }
69
Derek Allardfcbb0262010-01-17 16:13:15 +000070 return number_format($num, $precision).' '.$unit;
Derek Allard2067d1a2008-11-13 22:59:24 +000071 }
72}
73
Derek Allardfcbb0262010-01-17 16:13:15 +000074
75
Derek Allard2067d1a2008-11-13 22:59:24 +000076/* End of file number_helper.php */
Derek Jonesa935c3f2008-06-19 17:42:55 +000077/* Location: ./system/helpers/number_helper.php */