PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/views/helpers/number.php

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