Number.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace Tools\Utility;
  3. use Cake\Core\Configure;
  4. use Cake\I18n\Number as CakeNumber;
  5. /**
  6. * Extend CakeNumber with a few important improvements:
  7. * - config setting for format()
  8. * - spacer char for currency (initially from https://github.com/cakephp/cakephp/pull/1148)
  9. * - signed values possible
  10. *
  11. */
  12. class Number extends CakeNumber {
  13. protected static $_currency = 'EUR';
  14. protected static $_symbolRight = '€';
  15. protected static $_symbolLeft = '';
  16. protected static $_decimals = ',';
  17. protected static $_thousands = '.';
  18. /**
  19. * Correct the defaul values according to localization
  20. *
  21. * @return void
  22. */
  23. public static function config($options = array()) {
  24. $config = $options + (array)Configure::read('Localization');
  25. foreach ($config as $key => $value) {
  26. $key = '_' . $key;
  27. if (!isset(static::${$key})) {
  28. continue;
  29. }
  30. static::${$key} = $value;
  31. }
  32. }
  33. /**
  34. * Convenience method to display the default currency
  35. *
  36. * @param float $amount
  37. * @param array $formatOptions
  38. * @return string
  39. */
  40. public static function money($amount, array $formatOptions = array()) {
  41. return static::currency($amount, null, $formatOptions);
  42. }
  43. /**
  44. * Format numeric values
  45. * should not be used for currencies
  46. * //TODO: automize per localeconv() ?
  47. *
  48. * @param float $number
  49. * @param array $options : currency=true/false, ... (leave empty for no special treatment)
  50. * @return string
  51. */
  52. public static function _format($number, array $formatOptions = array()) {
  53. if (!is_numeric($number)) {
  54. $default = '---';
  55. if (!empty($options['default'])) {
  56. $default = $options['default'];
  57. }
  58. return $default;
  59. }
  60. if ($formatOptions === false) {
  61. $formatOptions = array();
  62. } elseif (!is_array($formatOptions)) {
  63. $formatOptions = array('places' => $formatOptions);
  64. }
  65. $options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => static::$_thousands, 'decimals' => static::$_decimals, 'escape' => false);
  66. $options = $formatOptions + $options;
  67. if (!empty($options['currency'])) {
  68. if (!empty(static::$_symbolRight)) {
  69. $options['after'] = ' ' . static::$_symbolRight;
  70. } elseif (!empty(static::$_symbolLeft)) {
  71. $options['before'] = static::$_symbolLeft . ' ';
  72. }
  73. }
  74. /*
  75. if ($spacer !== false) {
  76. $spacer = ($spacer === true) ? ' ' : $spacer;
  77. if ((string)$before !== '') {
  78. $before .= $spacer;
  79. }
  80. if ((string)$after !== '') {
  81. $after = $spacer . $after;
  82. }
  83. }
  84. */
  85. if ($options['places'] < 0) {
  86. $number = round($number, $options['places']);
  87. }
  88. $sign = '';
  89. if ($number > 0 && !empty($options['signed'])) {
  90. $sign = '+';
  91. }
  92. if (isset($options['signed'])) {
  93. unset($options['signed']);
  94. }
  95. return $sign . parent::format($number, $options);
  96. }
  97. public static function format($number, array $options = array()) {
  98. $defaults = array(
  99. 'positive' => '+', 'signed' => false
  100. );
  101. $options += $defaults;
  102. $sign = '';
  103. if ($number > 0 && !empty($options['signed'])) {
  104. $sign = '+';
  105. }
  106. if (isset($options['signed'])) {
  107. unset($options['signed']);
  108. }
  109. return $sign . parent::format($number, $options);
  110. }
  111. /**
  112. * Overwrite to allow
  113. *
  114. * - signed: true/false
  115. *
  116. * @param float $number
  117. * @param string $currency
  118. * @param array $options
  119. * @return string
  120. */
  121. public static function currency($number, $currency = null, array $options = array()) {
  122. $defaults = array(
  123. 'positive' => '+', 'signed' => false
  124. );
  125. $options += $defaults;
  126. $sign = '';
  127. if ($number > 0 && !empty($options['signed'])) {
  128. $sign = $options['positive'];
  129. }
  130. return $sign . parent::currency($number, $currency, $options);
  131. }
  132. /**
  133. * Returns a formatted-for-humans file size.
  134. *
  135. * @param int $size Size in bytes
  136. * @return string Human readable size
  137. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
  138. */
  139. public static function _toReadableSize($size, $decimals = '.') {
  140. $size = parent::toReadableSize($size);
  141. if ($decimals !== '.') {
  142. $size = str_replace('.', $decimals, $size);
  143. }
  144. return $size;
  145. }
  146. /**
  147. * Get the rounded average.
  148. *
  149. * @param array $values: int or float values
  150. * @param int $precision
  151. * @return float Average
  152. */
  153. public static function average($values, $precision = 0) {
  154. if (empty($values)) {
  155. return 0.0;
  156. }
  157. return round(array_sum($values) / count($values), $precision);
  158. }
  159. /**
  160. * Round value.
  161. *
  162. * @param float $number
  163. * @param float $increment
  164. * @return float result
  165. */
  166. public static function roundTo($number, $increments = 1.0) {
  167. $precision = static::getDecimalPlaces($increments);
  168. $res = round($number, $precision);
  169. if ($precision <= 0) {
  170. $res = (int)$res;
  171. }
  172. return $res;
  173. }
  174. /**
  175. * Round value up.
  176. *
  177. * @param float $number
  178. * @param int $increment
  179. * @return float result
  180. */
  181. public static function roundUpTo($number, $increments = 1) {
  182. return (ceil($number / $increments) * $increments);
  183. }
  184. /**
  185. * Round value down.
  186. *
  187. * @param float $number
  188. * @param int $increment
  189. * @return float result
  190. */
  191. public static function roundDownTo($number, $increments = 1) {
  192. return (floor($number / $increments) * $increments);
  193. }
  194. /**
  195. * Get decimal places
  196. *
  197. * @param float $number
  198. * @return int decimalPlaces
  199. */
  200. public static function getDecimalPlaces($number) {
  201. $decimalPlaces = 0;
  202. while ($number > 1 && $number != 0) {
  203. $number /= 10;
  204. $decimalPlaces -= 1;
  205. }
  206. while ($number < 1 && $number != 0) {
  207. $number *= 10;
  208. $decimalPlaces += 1;
  209. }
  210. return $decimalPlaces;
  211. }
  212. /**
  213. * Returns the English ordinal suffix (th, st, nd, etc) of a number.
  214. *
  215. * echo NumberLib::ordinal(2); // "nd"
  216. * echo NumberLib::ordinal(10); // "th"
  217. * echo NumberLib::ordinal(33); // "rd"
  218. *
  219. * @param int $number
  220. * @return string
  221. */
  222. public static function ordinal($number) {
  223. if ($number % 100 > 10 && $number % 100 < 14) {
  224. return 'th';
  225. }
  226. switch ($number % 10) {
  227. case 1:
  228. return 'st';
  229. case 2:
  230. return 'nd';
  231. case 3:
  232. return 'rd';
  233. default:
  234. return 'th';
  235. }
  236. }
  237. /**
  238. * Can compare two float values
  239. *
  240. * @link http://php.net/manual/en/language.types.float.php
  241. * @param float $x
  242. * @param float $y
  243. * @param float $precision
  244. * @return bool
  245. */
  246. public static function isFloatEqual($x, $y, $precision = 0.0000001) {
  247. return ($x + $precision >= $y) && ($x - $precision <= $y);
  248. }
  249. /**
  250. * Get the settings for a specific formatName
  251. *
  252. * @param string $formatName (EUR, ...)
  253. * @return array currencySettings or null on failure
  254. */
  255. public static function getFormat($formatName) {
  256. if (!isset(static::$_currencies[$formatName])) {
  257. return null;
  258. }
  259. return static::$_currencies[$formatName];
  260. }
  261. }