NumberLib.php 7.7 KB

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