/upload/views/helpers/number.php

https://github.com/vprorok/pny · PHP · 235 lines · 140 code · 19 blank · 76 comment · 23 complexity · 5726fa680ad1ec5b71409500f5edc099 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id: number.php 7296 2008-06-27 09:09:03Z gwoo $ */
  3. /**
  4. * Number Helper.
  5. *
  6. * Methods to make numbers more readable.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The MIT License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.cake.libs.view.helpers
  23. * @since CakePHP(tm) v 0.10.0.1076
  24. * @version $Revision: 7296 $
  25. * @modifiedby $LastChangedBy: gwoo $
  26. * @lastmodified $Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. /**
  30. * Number helper library.
  31. *
  32. * Methods to make numbers more readable.
  33. *
  34. * @package cake
  35. * @subpackage cake.cake.libs.view.helpers
  36. */
  37. class NumberHelper extends AppHelper {
  38. /**
  39. * Formats a number with a level of precision.
  40. *
  41. * @param float $number A floating point number.
  42. * @param integer $precision The precision of the returned number.
  43. * @return float Enter description here...
  44. * @static
  45. */
  46. function precision($number, $precision = 3) {
  47. return sprintf("%01.{$precision}f", $number);
  48. }
  49. /**
  50. * Returns a formatted-for-humans file size.
  51. *
  52. * @param integer $length Size in bytes
  53. * @return string Human readable size
  54. * @static
  55. */
  56. function toReadableSize($size) {
  57. switch($size) {
  58. case 0:
  59. return '0 Bytes';
  60. case 1:
  61. return '1 Byte';
  62. case $size < 1024:
  63. return $size . ' Bytes';
  64. case $size < 1024 * 1024:
  65. return $this->precision($size / 1024, 0) . ' KB';
  66. case $size < 1024 * 1024 * 1024:
  67. return $this->precision($size / 1024 / 1024, 2) . ' MB';
  68. case $size < 1024 * 1024 * 1024 * 1024:
  69. return $this->precision($size / 1024 / 1024 / 1024, 2) . ' GB';
  70. case $size < 1024 * 1024 * 1024 * 1024 * 1024:
  71. return $this->precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB';
  72. }
  73. }
  74. /**
  75. * Formats a number into a percentage string.
  76. *
  77. * @param float $number A floating point number
  78. * @param integer $precision The precision of the returned number
  79. * @return string Percentage string
  80. * @static
  81. */
  82. function toPercentage($number, $precision = 2) {
  83. return $this->precision($number, $precision) . '%';
  84. }
  85. /**
  86. * Formats a number into a currency format.
  87. *
  88. * @param float $number A floating point number
  89. * @param integer $options if int then places, if string then before, if (,.-) then use it
  90. * or array with places and before keys
  91. * @return string formatted number
  92. * @static
  93. */
  94. function format($number, $options = false) {
  95. $places = 0;
  96. if (is_int($options)) {
  97. $places = $options;
  98. }
  99. $separators = array(',', '.', '-', ':');
  100. $before = $after = null;
  101. if (is_string($options) && !in_array($options, $separators)) {
  102. $before = $options;
  103. }
  104. $thousands = ',';
  105. if (!is_array($options) && in_array($options, $separators)) {
  106. $thousands = $options;
  107. }
  108. $decimals = '.';
  109. if (!is_array($options) && in_array($options, $separators)) {
  110. $decimals = $options;
  111. }
  112. $escape = true;
  113. if (is_array($options)) {
  114. $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
  115. extract($options);
  116. }
  117. $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
  118. if ($escape) {
  119. return h($out);
  120. }
  121. return $out;
  122. }
  123. /**
  124. * Formats a number into a currency format.
  125. *
  126. * @param float $number
  127. * @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
  128. * set at least 'before' and 'after' options.
  129. * @param array $options
  130. * @return string Number formatted as a currency.
  131. */
  132. function currency($number, $currency = 'USD', $options = array()) {
  133. $default = array(
  134. 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
  135. 'decimals' => '.','negative' => '()', 'escape' => true
  136. );
  137. $currencies = $this->_getCurrencies();
  138. if (isset($currencies[$currency])) {
  139. $default = $currencies[$currency];
  140. } elseif (is_string($currency)) {
  141. $options['before'] = $currency;
  142. }
  143. $options = array_merge($default, $options);
  144. $result = null;
  145. if ($number == 0 ) {
  146. if ($options['zero'] !== 0 ) {
  147. return $options['zero'];
  148. }
  149. $options['after'] = null;
  150. } elseif ($number < 1 && $number > -1 ) {
  151. if(Configure::read('App.noCents') == true) {
  152. $options['after'] = null;
  153. } else {
  154. if(!empty($options['after'])){
  155. $multiply = intval('1' . str_pad('', $options['places'], '0'));
  156. $number = $number * $multiply;
  157. $options['before'] = null;
  158. $options['places'] = null;
  159. }
  160. }
  161. } else {
  162. $options['after'] = null;
  163. }
  164. $abs = abs($number);
  165. $result = $this->format($abs, $options);
  166. if ($number < 0 ) {
  167. if($options['negative'] == '()') {
  168. $result = '(' . $result .')';
  169. } else {
  170. $result = $options['negative'] . $result;
  171. }
  172. }
  173. return $result;
  174. }
  175. function currencySign($currency) {
  176. $currencies=$this->_getCurrencies();
  177. return $currencies[$currency]['before'];
  178. }
  179. function _getCurrencies() {
  180. return array(
  181. 'USD' => array(
  182. 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  183. 'decimals' => '.', 'negative' => '()', 'escape' => true
  184. ),
  185. 'GBP' => array(
  186. 'before'=>'&#163;', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  187. 'decimals' => '.', 'negative' => '()','escape' => false
  188. ),
  189. 'EUR' => array(
  190. 'before'=>'&#8364;', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
  191. 'decimals' => ',', 'negative' => '()', 'escape' => false
  192. ),
  193. 'AUD' => array(
  194. 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  195. 'decimals' => '.', 'negative' => '()', 'escape' => true
  196. ),
  197. 'NZD' => array(
  198. 'before' => 'NZ$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  199. 'decimals' => '.', 'negative' => '()', 'escape' => true
  200. ),
  201. 'PLN' => array(
  202. 'before'=>'z&#322;', 'after' => '', 'zero' => 0, 'places' => 2, 'thousands' => '',
  203. 'decimals' => ',', 'negative' => '()', 'escape' => false
  204. ),
  205. 'LEI' => array(
  206. 'before'=>'', 'after' => 'LEI', 'zero' => 0, 'places' => 2, 'thousands' => '',
  207. 'decimals' => ',', 'negative' => '()', 'escape' => false
  208. ),
  209. 'NOK' => array(
  210. 'before' => 'kr ', 'after' => '', 'zero' => 0, 'places' => 2, 'thousands' => ',',
  211. 'decimals' => '.', 'negative' => '()', 'escape' => true
  212. )
  213. );
  214. }
  215. }
  216. ?>