/halogy/helpers/number_helper.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 75 lines · 35 code · 7 blank · 33 comment · 3 complexity · 819000fbf81939c48c4560a8d031da21 MD5 · raw file

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