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: 30: 31: 32: 33:
34: class NumberHelper extends AppHelper {
35: 36: 37: 38: 39: 40: 41: 42:
43: function precision($number, $precision = 3) {
44: return sprintf("%01.{$precision}f", $number);
45: }
46: 47: 48: 49: 50: 51: 52:
53: function toReadableSize($size) {
54: switch (true) {
55: case $size < 1024:
56: return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
57: case round($size / 1024) < 1024:
58: return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
59: case round($size / 1024 / 1024, 2) < 1024:
60: return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
61: case round($size / 1024 / 1024 / 1024, 2) < 1024:
62: return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
63: default:
64: return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
65: }
66: }
67: 68: 69: 70: 71: 72: 73: 74:
75: function toPercentage($number, $precision = 2) {
76: return $this->precision($number, $precision) . '%';
77: }
78: 79: 80: 81: 82: 83: 84: 85: 86:
87: function format($number, $options = false) {
88: $places = 0;
89: if (is_int($options)) {
90: $places = $options;
91: }
92:
93: $separators = array(',', '.', '-', ':');
94:
95: $before = $after = null;
96: if (is_string($options) && !in_array($options, $separators)) {
97: $before = $options;
98: }
99: $thousands = ',';
100: if (!is_array($options) && in_array($options, $separators)) {
101: $thousands = $options;
102: }
103: $decimals = '.';
104: if (!is_array($options) && in_array($options, $separators)) {
105: $decimals = $options;
106: }
107:
108: $escape = true;
109: if (is_array($options)) {
110: $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
111: extract($options);
112: }
113:
114: $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
115:
116: if ($escape) {
117: return h($out);
118: }
119: return $out;
120: }
121: 122: 123: 124: 125: 126: 127: 128: 129:
130: function currency($number, $currency = 'USD', $options = array()) {
131: $default = array(
132: 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
133: 'decimals' => '.','negative' => '()', 'escape' => true
134: );
135: $currencies = array(
136: 'USD' => array(
137: 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
138: 'decimals' => '.', 'negative' => '()', 'escape' => true
139: ),
140: 'GBP' => array(
141: 'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
142: 'decimals' => '.', 'negative' => '()','escape' => false
143: ),
144: 'EUR' => array(
145: 'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
146: 'decimals' => ',', 'negative' => '()', 'escape' => false
147: )
148: );
149:
150: if (isset($currencies[$currency])) {
151: $default = $currencies[$currency];
152: } elseif (is_string($currency)) {
153: $options['before'] = $currency;
154: }
155:
156: $options = array_merge($default, $options);
157:
158: $result = null;
159:
160: if ($number == 0 ) {
161: if ($options['zero'] !== 0 ) {
162: return $options['zero'];
163: }
164: $options['after'] = null;
165: } elseif ($number < 1 && $number > -1 ) {
166: $multiply = intval('1' . str_pad('', $options['places'], '0'));
167: $number = $number * $multiply;
168: $options['before'] = null;
169: $options['places'] = null;
170: } elseif (empty($options['before'])) {
171: $options['before'] = null;
172: } else {
173: $options['after'] = null;
174: }
175:
176: $abs = abs($number);
177: $result = $this->format($abs, $options);
178:
179: if ($number < 0 ) {
180: if ($options['negative'] == '()') {
181: $result = '(' . $result .')';
182: } else {
183: $result = $options['negative'] . $result;
184: }
185: }
186: return $result;
187: }
188: }
189: ?>