number_helper.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Number Helpers
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/number_helper.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Formats a numbers as bytes, based on size, and adds the appropriate suffix
  29. *
  30. * @access public
  31. * @param mixed // will be cast as int
  32. * @return string
  33. */
  34. if ( ! function_exists('byte_format'))
  35. {
  36. function byte_format($num, $precision = 1)
  37. {
  38. $CI =& get_instance();
  39. $CI->lang->load('number');
  40. if ($num >= 1000000000000)
  41. {
  42. $num = round($num / 1099511627776, $precision);
  43. $unit = $CI->lang->line('terabyte_abbr');
  44. }
  45. elseif ($num >= 1000000000)
  46. {
  47. $num = round($num / 1073741824, $precision);
  48. $unit = $CI->lang->line('gigabyte_abbr');
  49. }
  50. elseif ($num >= 1000000)
  51. {
  52. $num = round($num / 1048576, $precision);
  53. $unit = $CI->lang->line('megabyte_abbr');
  54. }
  55. elseif ($num >= 1000)
  56. {
  57. $num = round($num / 1024, $precision);
  58. $unit = $CI->lang->line('kilobyte_abbr');
  59. }
  60. else
  61. {
  62. $unit = $CI->lang->line('bytes');
  63. return number_format($num).' '.$unit;
  64. }
  65. return number_format($num, $precision).' '.$unit;
  66. }
  67. }
  68. /* End of file number_helper.php */
  69. /* Location: ./system/helpers/number_helper.php */