NumberLib.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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',
  118. 'negative' => '-', 'positive'=> '+', 'escape' => true,
  119. 'decimals' => self::$_decimalPoint, 'thousands' => self::$_thousandsPoint,
  120. );
  121. $options = am($options, $formatOptions);
  122. if (!empty($options['wholeSymbol'])) {
  123. if ($options['wholePosition'] == 'after') {
  124. $options['wholeSymbol'] = ' ' . self::$_symbolRight;
  125. } elseif ($options['wholePosition'] == 'before') {
  126. $options['wholeSymbol'] = self::$_symbolLeft . ' ';
  127. }
  128. }
  129. $sign = '';
  130. if ($number > 0 && !empty($options['signed'])) {
  131. $sign = $options['positive'];
  132. }
  133. return $sign . parent::currency($number, $currency, $options);
  134. }
  135. /**
  136. * Formats a number with a level of precision.
  137. *
  138. * @param float $number A floating point number.
  139. * @param integer $precision The precision of the returned number.
  140. * @param string $decimals
  141. * @return float Formatted float.
  142. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
  143. */
  144. public static function precision($number, $precision = 3, $decimals = '.') {
  145. $number = parent::precision($number, $precision);
  146. if ($decimals != '.' && $precision > 0) {
  147. $number = str_replace('.', $decimals, $number);
  148. }
  149. return $number;
  150. }
  151. /**
  152. * Returns a formatted-for-humans file size.
  153. *
  154. * @param integer $size Size in bytes
  155. * @return string Human readable size
  156. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  157. */
  158. public static function toReadableSize($size, $decimals = '.') {
  159. $size = parent::toReadableSize($size);
  160. if ($decimals != '.') {
  161. $size = str_replace('.', $decimals, $size);
  162. }
  163. return $size;
  164. }
  165. /**
  166. * Formats a number into a percentage string.
  167. *
  168. * @param float $number A floating point number
  169. * @param integer $precision The precision of the returned number
  170. * @param string $decimals
  171. * @return string Percentage string
  172. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
  173. */
  174. public static function toPercentage($number, $precision = 2, $decimals = '.') {
  175. return self::precision($number, $precision, $decimals) . '%';
  176. }
  177. /**
  178. * get the rounded average
  179. * @param array $values: int or float values
  180. * @param int $precision
  181. * @return int $average
  182. * 2009-09-05 ms
  183. */
  184. public static function average($values, $precision = 0) {
  185. $average = round(array_sum($values) / count($values), $precision);
  186. return $average;
  187. }
  188. /**
  189. * @access public
  190. * @param float $number
  191. * @param float $increment
  192. * @return float $result
  193. * 2011-04-14 lb
  194. */
  195. public static function roundTo($number, $increments = 1.0) {
  196. $precision = self::getDecimalPlaces($increments);
  197. $res = round($number, $precision);
  198. if ($precision <= 0) {
  199. $res = (int)$res;
  200. }
  201. return $res;
  202. }
  203. /**
  204. * @access public
  205. * @param float $number
  206. * @param int $increment
  207. * @return float $result
  208. * 2011-04-14 lb
  209. */
  210. public static function roundUpTo($number, $increments = 1) {
  211. return (ceil($number / $increments) * $increments);
  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 roundDownTo($number, $increments = 1) {
  221. return (floor($number / $increments) * $increments);
  222. }
  223. /**
  224. * @access public
  225. * @param float $number
  226. * @return int $decimalPlaces
  227. * 2011-04-15 lb
  228. */
  229. public static function getDecimalPlaces($number) {
  230. $decimalPlaces = 0;
  231. while ($number > 1 && $number != 0) {
  232. $number /= 10;
  233. $decimalPlaces -= 1;
  234. }
  235. while ($number < 1 && $number != 0) {
  236. $number *= 10;
  237. $decimalPlaces += 1;
  238. }
  239. return $decimalPlaces;
  240. }
  241. /**
  242. * Returns the English ordinal suffix (th, st, nd, etc) of a number.
  243. *
  244. * echo 2, Num::ordinal(2); // "2nd"
  245. * echo 10, Num::ordinal(10); // "10th"
  246. * echo 33, Num::ordinal(33); // "33rd"
  247. *
  248. * @param integer number
  249. * @return string
  250. */
  251. public static function ordinal($number) {
  252. if ($number % 100 > 10 and $number % 100 < 14) {
  253. return 'th';
  254. }
  255. switch ($number % 10) {
  256. case 1:
  257. return 'st';
  258. case 2:
  259. return 'nd';
  260. case 3:
  261. return 'rd';
  262. default:
  263. return 'th';
  264. }
  265. }
  266. /**
  267. * Can compare two float values
  268. * @link http://php.net/manual/en/language.types.float.php
  269. * @return boolean
  270. */
  271. public static function isFloatEqual($x, $y, $precision = 0.0000001) {
  272. return ($x+$precision >= $y) && ($x-$precision <= $y);
  273. }
  274. }