NumberLib.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_array($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. 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. if ($spacer !== false) {
  82. $spacer = ($spacer === true) ? ' ' : $spacer;
  83. if ((string)$before !== '') {
  84. $before .= $spacer;
  85. }
  86. if ((string)$after !== '') {
  87. $after = $spacer . $after;
  88. }
  89. }
  90. */
  91. if ($options['places'] < 0) {
  92. $number = round($number, $options['places']);
  93. }
  94. $sign = '';
  95. if ($number > 0 && !empty($options['signed'])) {
  96. $sign = '+';
  97. }
  98. if (isset($options['signed'])) {
  99. unset($options['signed']);
  100. }
  101. return $sign . parent::format($number, $options);
  102. }
  103. /**
  104. * Correct the default for European countries
  105. * 2012-04-08 ms
  106. */
  107. public static function currency($number, $currency = null, $formatOptions = array()) {
  108. if ($currency === null) {
  109. $currency = self::$_currency;
  110. }
  111. $options = array(
  112. 'wholeSymbol' => self::$_symbolRight, 'wholePosition' => 'after',
  113. 'negative' => '-', 'positive'=> '+', 'escape' => true,
  114. 'decimals' => self::$_decimalPoint, 'thousands' => self::$_thousandsPoint,
  115. );
  116. $options = array_merge($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. }