1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23: 24: 25: 26: 27: 28:
29: class CakeNumber {
30:
31: 32: 33: 34: 35: 36:
37: protected static $_currencies = array(
38: 'AUD' => array(
39: 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
40: 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
41: 'fractionExponent' => 2
42: ),
43: 'CAD' => array(
44: 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
45: 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
46: 'fractionExponent' => 2
47: ),
48: 'USD' => array(
49: 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after',
50: 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
51: 'fractionExponent' => 2
52: ),
53: 'EUR' => array(
54: 'wholeSymbol' => '€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
55: 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => true,
56: 'fractionExponent' => 0
57: ),
58: 'GBP' => array(
59: 'wholeSymbol' => '£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after',
60: 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
61: 'fractionExponent' => 2
62: ),
63: 'JPY' => array(
64: 'wholeSymbol' => '¥', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
65: 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
66: 'fractionExponent' => 0
67: ),
68: );
69:
70: 71: 72: 73: 74:
75: protected static $_currencyDefaults = array(
76: 'wholeSymbol' => '', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after',
77: 'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true,
78: 'fractionExponent' => 2
79: );
80:
81: 82: 83: 84: 85:
86: protected static $_defaultCurrency = 'USD';
87:
88: 89: 90: 91: 92:
93: protected static $_numberFormatSupport = null;
94:
95: 96: 97: 98: 99: 100: 101: 102:
103: public static function precision($value, $precision = 3) {
104: return sprintf("%01.{$precision}f", $value);
105: }
106:
107: 108: 109: 110: 111: 112: 113:
114: public static function toReadableSize($size) {
115: switch (true) {
116: case $size < 1024:
117: return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
118: case round($size / 1024) < 1024:
119: return __d('cake', '%s KB', self::precision($size / 1024, 0));
120: case round($size / 1024 / 1024, 2) < 1024:
121: return __d('cake', '%s MB', self::precision($size / 1024 / 1024, 2));
122: case round($size / 1024 / 1024 / 1024, 2) < 1024:
123: return __d('cake', '%s GB', self::precision($size / 1024 / 1024 / 1024, 2));
124: default:
125: return __d('cake', '%s TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2));
126: }
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137:
138: public static function fromReadableSize($size, $default = false) {
139: if (ctype_digit($size)) {
140: return (int)$size;
141: }
142: $size = strtoupper($size);
143:
144: $l = -2;
145: $i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
146: if ($i === false) {
147: $l = -1;
148: $i = array_search(substr($size, -1), array('K', 'M', 'G', 'T', 'P'));
149: }
150: if ($i !== false) {
151: $size = substr($size, 0, $l);
152: return $size * pow(1024, $i + 1);
153: }
154:
155: if (substr($size, -1) === 'B' && ctype_digit(substr($size, 0, -1))) {
156: $size = substr($size, 0, -1);
157: return (int)$size;
158: }
159:
160: if ($default !== false) {
161: return $default;
162: }
163: throw new CakeException(__d('cake_dev', 'No unit type.'));
164: }
165:
166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
179: public static function toPercentage($value, $precision = 2, $options = array()) {
180: $options += array('multiply' => false);
181: if ($options['multiply']) {
182: $value *= 100;
183: }
184: return self::precision($value, $precision) . '%';
185: }
186:
187: 188: 189: 190: 191: 192: 193: 194: 195:
196: public static function format($value, $options = false) {
197: $places = 0;
198: if (is_int($options)) {
199: $places = $options;
200: }
201:
202: $separators = array(',', '.', '-', ':');
203:
204: $before = $after = null;
205: if (is_string($options) && !in_array($options, $separators)) {
206: $before = $options;
207: }
208: $thousands = ',';
209: if (!is_array($options) && in_array($options, $separators)) {
210: $thousands = $options;
211: }
212: $decimals = '.';
213: if (!is_array($options) && in_array($options, $separators)) {
214: $decimals = $options;
215: }
216:
217: $escape = true;
218: if (is_array($options)) {
219: $defaults = array('before' => '$', 'places' => 2, 'thousands' => ',', 'decimals' => '.');
220: $options += $defaults;
221: extract($options);
222: }
223:
224: $value = self::_numberFormat($value, $places, '.', '');
225: $out = $before . self::_numberFormat($value, $places, $decimals, $thousands) . $after;
226:
227: if ($escape) {
228: return h($out);
229: }
230: return $out;
231: }
232:
233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249:
250: public static function formatDelta($value, $options = array()) {
251: $places = isset($options['places']) ? $options['places'] : 0;
252: $value = self::_numberFormat($value, $places, '.', '');
253: $sign = $value > 0 ? '+' : '';
254: $options['before'] = isset($options['before']) ? $options['before'] . $sign : $sign;
255: return self::format($value, $options);
256: }
257:
258: 259: 260: 261: 262: 263: 264: 265: 266:
267: protected static function _numberFormat($value, $places = 0, $decimals = '.', $thousands = ',') {
268: if (!isset(self::$_numberFormatSupport)) {
269: self::$_numberFormatSupport = version_compare(PHP_VERSION, '5.4.0', '>=');
270: }
271: if (self::$_numberFormatSupport) {
272: return number_format($value, $places, $decimals, $thousands);
273: }
274: $value = number_format($value, $places, '.', '');
275: $after = '';
276: $foundDecimal = strpos($value, '.');
277: if ($foundDecimal !== false) {
278: $after = substr($value, $foundDecimal);
279: $value = substr($value, 0, $foundDecimal);
280: }
281: while (($foundThousand = preg_replace('/(\d+)(\d\d\d)/', '\1 \2', $value)) !== $value) {
282: $value = $foundThousand;
283: }
284: $value .= $after;
285: return strtr($value, array(' ' => $thousands, '.' => $decimals));
286: }
287:
288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324:
325: public static function currency($value, $currency = null, $options = array()) {
326: $default = self::$_currencyDefaults;
327: if ($currency === null) {
328: $currency = self::defaultCurrency();
329: }
330:
331: if (isset(self::$_currencies[$currency])) {
332: $default = self::$_currencies[$currency];
333: } elseif (is_string($currency)) {
334: $options['before'] = $currency;
335: }
336:
337: $options = array_merge($default, $options);
338:
339: if (isset($options['before']) && $options['before'] !== '') {
340: $options['wholeSymbol'] = $options['before'];
341: }
342: if (isset($options['after']) && !$options['after'] !== '') {
343: $options['fractionSymbol'] = $options['after'];
344: }
345:
346: $result = $options['before'] = $options['after'] = null;
347:
348: $symbolKey = 'whole';
349: $value = (float)$value;
350: if (!$value) {
351: if ($options['zero'] !== 0) {
352: return $options['zero'];
353: }
354: } elseif ($value < 1 && $value > -1) {
355: if ($options['fractionSymbol'] !== false) {
356: $multiply = pow(10, $options['fractionExponent']);
357: $value = $value * $multiply;
358: $options['places'] = null;
359: $symbolKey = 'fraction';
360: }
361: }
362:
363: $position = $options[$symbolKey . 'Position'] !== 'after' ? 'before' : 'after';
364: $options[$position] = $options[$symbolKey . 'Symbol'];
365:
366: $abs = abs($value);
367: $result = self::format($abs, $options);
368:
369: if ($value < 0) {
370: if ($options['negative'] === '()') {
371: $result = '(' . $result . ')';
372: } else {
373: $result = $options['negative'] . $result;
374: }
375: }
376: return $result;
377: }
378:
379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397:
398: public static function addFormat($formatName, $options) {
399: self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
400: }
401:
402: 403: 404: 405: 406: 407: 408:
409: public static function defaultCurrency($currency = null) {
410: if ($currency) {
411: self::$_defaultCurrency = $currency;
412: }
413: return self::$_defaultCurrency;
414: }
415:
416: }
417: