NumberLib.php 7.8 KB

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