| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: number.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Number Helper.
|
|
|
5 |
*
|
|
|
6 |
* Methods to make numbers more readable.
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
21 |
* @since CakePHP(tm) v 0.10.0.1076
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Number helper library.
|
|
|
29 |
*
|
|
|
30 |
* Methods to make numbers more readable.
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.libs.view.helpers
|
|
|
34 |
*/
|
|
|
35 |
class NumberHelper extends AppHelper {
|
|
|
36 |
/**
|
|
|
37 |
* Formats a number with a level of precision.
|
|
|
38 |
*
|
|
|
39 |
* @param float $number A floating point number.
|
|
|
40 |
* @param integer $precision The precision of the returned number.
|
|
|
41 |
* @return float Enter description here...
|
|
|
42 |
* @static
|
|
|
43 |
*/
|
|
|
44 |
function precision($number, $precision = 3) {
|
|
|
45 |
return sprintf("%01.{$precision}f", $number);
|
|
|
46 |
}
|
|
|
47 |
/**
|
|
|
48 |
* Returns a formatted-for-humans file size.
|
|
|
49 |
*
|
|
|
50 |
* @param integer $length Size in bytes
|
|
|
51 |
* @return string Human readable size
|
|
|
52 |
* @static
|
|
|
53 |
*/
|
|
|
54 |
function toReadableSize($size) {
|
|
|
55 |
switch ($size) {
|
|
|
56 |
case 0:
|
|
|
57 |
return '0 Bytes';
|
|
|
58 |
case 1:
|
|
|
59 |
return '1 Byte';
|
|
|
60 |
case $size < 1024:
|
|
|
61 |
return $size . ' Bytes';
|
|
|
62 |
case round($size / 1024) < 1024:
|
|
|
63 |
return $this->precision($size / 1024, 0) . ' KB';
|
|
|
64 |
case round($size / 1024 / 1024, 2) < 1024:
|
|
|
65 |
return $this->precision($size / 1024 / 1024, 2) . ' MB';
|
|
|
66 |
case round($size / 1024 / 1024 / 1024, 2) < 1024:
|
|
|
67 |
return $this->precision($size / 1024 / 1024 / 1024, 2) . ' GB';
|
|
|
68 |
default:
|
|
|
69 |
return $this->precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB';
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
/**
|
|
|
73 |
* Formats a number into a percentage string.
|
|
|
74 |
*
|
|
|
75 |
* @param float $number A floating point number
|
|
|
76 |
* @param integer $precision The precision of the returned number
|
|
|
77 |
* @return string Percentage string
|
|
|
78 |
* @static
|
|
|
79 |
*/
|
|
|
80 |
function toPercentage($number, $precision = 2) {
|
|
|
81 |
return $this->precision($number, $precision) . '%';
|
|
|
82 |
}
|
|
|
83 |
/**
|
|
|
84 |
* Formats a number into a currency format.
|
|
|
85 |
*
|
|
|
86 |
* @param float $number A floating point number
|
|
|
87 |
* @param integer $options if int then places, if string then before, if (,.-) then use it
|
|
|
88 |
* or array with places and before keys
|
|
|
89 |
* @return string formatted number
|
|
|
90 |
* @static
|
|
|
91 |
*/
|
|
|
92 |
function format($number, $options = false) {
|
|
|
93 |
$places = 0;
|
|
|
94 |
if (is_int($options)) {
|
|
|
95 |
$places = $options;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$separators = array(',', '.', '-', ':');
|
|
|
99 |
|
|
|
100 |
$before = $after = null;
|
|
|
101 |
if (is_string($options) && !in_array($options, $separators)) {
|
|
|
102 |
$before = $options;
|
|
|
103 |
}
|
|
|
104 |
$thousands = ',';
|
|
|
105 |
if (!is_array($options) && in_array($options, $separators)) {
|
|
|
106 |
$thousands = $options;
|
|
|
107 |
}
|
|
|
108 |
$decimals = '.';
|
|
|
109 |
if (!is_array($options) && in_array($options, $separators)) {
|
|
|
110 |
$decimals = $options;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$escape = true;
|
|
|
114 |
if (is_array($options)) {
|
|
|
115 |
$options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
|
|
|
116 |
extract($options);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
$out = $before . number_format($number, $places, $decimals, $thousands) . $after;
|
|
|
120 |
|
|
|
121 |
if ($escape) {
|
|
|
122 |
return h($out);
|
|
|
123 |
}
|
|
|
124 |
return $out;
|
|
|
125 |
}
|
|
|
126 |
/**
|
|
|
127 |
* Formats a number into a currency format.
|
|
|
128 |
*
|
|
|
129 |
* @param float $number
|
|
|
130 |
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
|
|
|
131 |
* set at least 'before' and 'after' options.
|
|
|
132 |
* @param array $options
|
|
|
133 |
* @return string Number formatted as a currency.
|
|
|
134 |
*/
|
|
|
135 |
function currency($number, $currency = 'USD', $options = array()) {
|
|
|
136 |
$default = array(
|
|
|
137 |
'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
|
|
|
138 |
'decimals' => '.','negative' => '()', 'escape' => true
|
|
|
139 |
);
|
|
|
140 |
$currencies = array(
|
|
|
141 |
'USD' => array(
|
|
|
142 |
'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
|
|
|
143 |
'decimals' => '.', 'negative' => '()', 'escape' => true
|
|
|
144 |
),
|
|
|
145 |
'GBP' => array(
|
|
|
146 |
'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
|
|
|
147 |
'decimals' => '.', 'negative' => '()','escape' => false
|
|
|
148 |
),
|
|
|
149 |
'EUR' => array(
|
|
|
150 |
'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
|
|
|
151 |
'decimals' => ',', 'negative' => '()', 'escape' => false
|
|
|
152 |
)
|
|
|
153 |
);
|
|
|
154 |
|
|
|
155 |
if (isset($currencies[$currency])) {
|
|
|
156 |
$default = $currencies[$currency];
|
|
|
157 |
} elseif (is_string($currency)) {
|
|
|
158 |
$options['before'] = $currency;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$options = array_merge($default, $options);
|
|
|
162 |
|
|
|
163 |
$result = null;
|
|
|
164 |
|
|
|
165 |
if ($number == 0 ) {
|
|
|
166 |
if ($options['zero'] !== 0 ) {
|
|
|
167 |
return $options['zero'];
|
|
|
168 |
}
|
|
|
169 |
$options['after'] = null;
|
|
|
170 |
} elseif ($number < 1 && $number > -1 ) {
|
|
|
171 |
$multiply = intval('1' . str_pad('', $options['places'], '0'));
|
|
|
172 |
$number = $number * $multiply;
|
|
|
173 |
$options['before'] = null;
|
|
|
174 |
$options['places'] = null;
|
|
|
175 |
} else {
|
|
|
176 |
$options['after'] = null;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
$abs = abs($number);
|
|
|
180 |
$result = $this->format($abs, $options);
|
|
|
181 |
|
|
|
182 |
if ($number < 0 ) {
|
|
|
183 |
if ($options['negative'] == '()') {
|
|
|
184 |
$result = '(' . $result .')';
|
|
|
185 |
} else {
|
|
|
186 |
$result = $options['negative'] . $result;
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
return $result;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
?>
|