NumberLib.php 7.7 KB

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