NumberLib.php 7.7 KB

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