NumberLib.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. App::uses('CakeNumber', 'Utility');
  3. /**
  4. * 2011-03-07 ms
  5. */
  6. class NumberLib extends CakeNumber {
  7. protected static $_currency = 'EUR';
  8. protected static $_symbolRight = '€';
  9. protected static $_symbolLeft = '';
  10. protected static $_decimals = ',';
  11. protected static $_thousands = '.';
  12. /**
  13. * Correct the defaul values according to localization
  14. *
  15. * @return void
  16. */
  17. public static function config($options = array()) {
  18. $config = $options + (array)Configure::read('Localization');
  19. foreach ($config as $key => $value) {
  20. $key = '_' . $key;
  21. if (!isset(self::${$key})) {
  22. continue;
  23. }
  24. self::${$key} = $value;
  25. }
  26. }
  27. /**
  28. * Display price (or was price if available)
  29. * Without allowNegative it will always default all non-positive values to 0
  30. *
  31. * @param price
  32. * @param specialPrice (outranks the price)
  33. * @param options
  34. * - places
  35. * - allowNegative (defaults to false - price needs to be > 0)
  36. *
  37. * @deprecated use currency()
  38. * @return string
  39. * 2011-07-30 ms
  40. */
  41. public static function price($price, $specialPrice = null, $formatOptions = array()) {
  42. if ($specialPrice !== null && $specialPrice > 0) {
  43. $val = $specialPrice;
  44. } elseif ($price > 0 || !empty($formatOptions['allowNegative'])) {
  45. $val = $price;
  46. } else {
  47. if (isset($formatOptions['default'])) {
  48. return $formatOptions['default'];
  49. }
  50. $val = max(0, $price);
  51. }
  52. return self::money($val, $formatOptions);
  53. }
  54. /**
  55. * Convinience method to display the default currency
  56. *
  57. * @return string
  58. * 2011-10-05 ms
  59. */
  60. public static function money($amount, $formatOptions = array()) {
  61. return self::currency($amount, null, $formatOptions);
  62. }
  63. /**
  64. * format numeric values
  65. * should not be used for currencies
  66. *
  67. * @param float $number
  68. * @param int $places (0 = int, 1..x places after dec, -1..-x places before dec)
  69. * @param array $option : currency=true/false, ... (leave empty for no special treatment)
  70. * //TODO: automize per localeconv() ?
  71. * 2009-04-03 ms
  72. */
  73. public static function format($number, $formatOptions = array()) {
  74. if (!is_numeric($number)) {
  75. $default = '---';
  76. if (!empty($options['default'])) {
  77. $default = $options['default'];
  78. }
  79. return $default;
  80. }
  81. if ($formatOptions === false) {
  82. $formatOptions = array();
  83. } elseif (!is_array($formatOptions)) {
  84. $formatOptions = array('places' => $formatOptions);
  85. }
  86. $options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => self::$_thousands, 'decimals' => self::$_decimals, 'escape' => false);
  87. $options = array_merge($options, $formatOptions);
  88. if (!empty($options['currency'])) {
  89. if (!empty(self::$_symbolRight)) {
  90. $options['after'] = ' ' . self::$_symbolRight;
  91. } elseif (!empty(self::$_symbolLeft)) {
  92. $options['before'] = self::$_symbolLeft . ' ';
  93. }
  94. }
  95. /*
  96. if ($spacer !== false) {
  97. $spacer = ($spacer === true) ? ' ' : $spacer;
  98. if ((string)$before !== '') {
  99. $before .= $spacer;
  100. }
  101. if ((string)$after !== '') {
  102. $after = $spacer . $after;
  103. }
  104. }
  105. */
  106. if ($options['places'] < 0) {
  107. $number = round($number, $options['places']);
  108. }
  109. $sign = '';
  110. if ($number > 0 && !empty($options['signed'])) {
  111. $sign = '+';
  112. }
  113. if (isset($options['signed'])) {
  114. unset($options['signed']);
  115. }
  116. return $sign . parent::format($number, $options);
  117. }
  118. /**
  119. * Correct the default for European countries
  120. * 2012-04-08 ms
  121. */
  122. public static function currency($number, $currency = null, $formatOptions = array()) {
  123. if ($currency === null) {
  124. $currency = self::$_currency;
  125. }
  126. $options = array(
  127. 'wholeSymbol' => self::$_symbolRight, 'wholePosition' => 'after',
  128. 'negative' => '-', 'positive'=> '+', 'escape' => true,
  129. 'decimals' => self::$_decimals, 'thousands' => self::$_thousands,
  130. );
  131. $options = array_merge($options, $formatOptions);
  132. if (!empty($options['wholeSymbol'])) {
  133. if ($options['wholePosition'] === 'after') {
  134. $options['wholeSymbol'] = ' ' . self::$_symbolRight;
  135. } elseif ($options['wholePosition'] === 'before') {
  136. $options['wholeSymbol'] = self::$_symbolLeft . ' ';
  137. }
  138. }
  139. $sign = '';
  140. if ($number > 0 && !empty($options['signed'])) {
  141. $sign = $options['positive'];
  142. }
  143. return $sign . parent::currency($number, $currency, $options);
  144. }
  145. /**
  146. * Formats a number with a level of precision.
  147. *
  148. * @param float $number A floating point number.
  149. * @param integer $precision The precision of the returned number.
  150. * @param string $decimals
  151. * @return float Formatted float.
  152. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
  153. */
  154. public static function precision($number, $precision = 3, $decimals = '.') {
  155. $number = parent::precision($number, $precision);
  156. if ($decimals !== '.' && $precision > 0) {
  157. $number = str_replace('.', $decimals, $number);
  158. }
  159. return $number;
  160. }
  161. /**
  162. * Returns a formatted-for-humans file size.
  163. *
  164. * @param integer $size Size in bytes
  165. * @return string Human readable size
  166. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  167. */
  168. public static function toReadableSize($size, $decimals = '.') {
  169. $size = parent::toReadableSize($size);
  170. if ($decimals !== '.') {
  171. $size = str_replace('.', $decimals, $size);
  172. }
  173. return $size;
  174. }
  175. /**
  176. * Formats a number into a percentage string.
  177. *
  178. * @param float $number A floating point number
  179. * @param integer $precision The precision of the returned number
  180. * @param string $decimals
  181. * @return string Percentage string
  182. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
  183. */
  184. public static function toPercentage($number, $precision = 2, $decimals = '.') {
  185. return self::precision($number, $precision, $decimals) . '%';
  186. }
  187. /**
  188. * get the rounded average
  189. * @param array $values: int or float values
  190. * @param int $precision
  191. * @return int $average
  192. * 2009-09-05 ms
  193. */
  194. public static function average($values, $precision = 0) {
  195. $average = round(array_sum($values) / count($values), $precision);
  196. return $average;
  197. }
  198. /**
  199. * @access public
  200. * @param float $number
  201. * @param float $increment
  202. * @return float $result
  203. * 2011-04-14 lb
  204. */
  205. public static function roundTo($number, $increments = 1.0) {
  206. $precision = self::getDecimalPlaces($increments);
  207. $res = round($number, $precision);
  208. if ($precision <= 0) {
  209. $res = (int)$res;
  210. }
  211. return $res;
  212. }
  213. /**
  214. * @access public
  215. * @param float $number
  216. * @param int $increment
  217. * @return float $result
  218. * 2011-04-14 lb
  219. */
  220. public static function roundUpTo($number, $increments = 1) {
  221. return (ceil($number / $increments) * $increments);
  222. }
  223. /**
  224. * @access public
  225. * @param float $number
  226. * @param int $increment
  227. * @return float $result
  228. * 2011-04-14 lb
  229. */
  230. public static function roundDownTo($number, $increments = 1) {
  231. return (floor($number / $increments) * $increments);
  232. }
  233. /**
  234. * @access public
  235. * @param float $number
  236. * @return int $decimalPlaces
  237. * 2011-04-15 lb
  238. */
  239. public static function getDecimalPlaces($number) {
  240. $decimalPlaces = 0;
  241. while ($number > 1 && $number != 0) {
  242. $number /= 10;
  243. $decimalPlaces -= 1;
  244. }
  245. while ($number < 1 && $number != 0) {
  246. $number *= 10;
  247. $decimalPlaces += 1;
  248. }
  249. return $decimalPlaces;
  250. }
  251. /**
  252. * Returns the English ordinal suffix (th, st, nd, etc) of a number.
  253. *
  254. * echo 2, Num::ordinal(2); // "2nd"
  255. * echo 10, Num::ordinal(10); // "10th"
  256. * echo 33, Num::ordinal(33); // "33rd"
  257. *
  258. * @param integer number
  259. * @return string
  260. */
  261. public static function ordinal($number) {
  262. if ($number % 100 > 10 and $number % 100 < 14) {
  263. return 'th';
  264. }
  265. switch ($number % 10) {
  266. case 1:
  267. return 'st';
  268. case 2:
  269. return 'nd';
  270. case 3:
  271. return 'rd';
  272. default:
  273. return 'th';
  274. }
  275. }
  276. /**
  277. * Can compare two float values
  278. * @link http://php.net/manual/en/language.types.float.php
  279. * @return boolean
  280. */
  281. public static function isFloatEqual($x, $y, $precision = 0.0000001) {
  282. return ($x + $precision >= $y) && ($x - $precision <= $y);
  283. }
  284. }