5
* Methods to make numbers more readable.
9
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://cakephp.org CakePHP(tm) Project
18
* @subpackage cake.cake.libs.view.helpers
19
* @since CakePHP(tm) v 0.10.0.1076
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
24
* Number helper library.
26
* Methods to make numbers more readable.
29
* @subpackage cake.cake.libs.view.helpers
30
* @link http://book.cakephp.org/view/1452/Number
32
class NumberHelper extends AppHelper {
35
* Currencies supported by the helper. You can add additional currency formats
36
* with NumberHelper::addFormat
41
var $_currencies = array(
43
'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
44
'decimals' => '.', 'negative' => '()', 'escape' => true
47
'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
48
'decimals' => '.', 'negative' => '()','escape' => false
51
'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
52
'decimals' => ',', 'negative' => '()', 'escape' => false
57
* Default options for currency formats
62
var $_currencyDefaults = array(
63
'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
64
'decimals' => '.','negative' => '()', 'escape' => true
68
* Formats a number with a level of precision.
70
* @param float $number A floating point number.
71
* @param integer $precision The precision of the returned number.
72
* @return float Formatted float.
74
* @link http://book.cakephp.org/view/1454/precision
76
function precision($number, $precision = 3) {
77
return sprintf("%01.{$precision}f", $number);
81
* Returns a formatted-for-humans file size.
83
* @param integer $length Size in bytes
84
* @return string Human readable size
86
* @link http://book.cakephp.org/view/1456/toReadableSize
88
function toReadableSize($size) {
91
return sprintf(__n('%d Byte', '%d Bytes', $size, true), $size);
92
case round($size / 1024) < 1024:
93
return sprintf(__('%d KB', true), $this->precision($size / 1024, 0));
94
case round($size / 1024 / 1024, 2) < 1024:
95
return sprintf(__('%.2f MB', true), $this->precision($size / 1024 / 1024, 2));
96
case round($size / 1024 / 1024 / 1024, 2) < 1024:
97
return sprintf(__('%.2f GB', true), $this->precision($size / 1024 / 1024 / 1024, 2));
99
return sprintf(__('%.2f TB', true), $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
104
* Formats a number into a percentage string.
106
* @param float $number A floating point number
107
* @param integer $precision The precision of the returned number
108
* @return string Percentage string
110
* @link http://book.cakephp.org/view/1455/toPercentage
112
function toPercentage($number, $precision = 2) {
113
return $this->precision($number, $precision) . '%';
117
* Formats a number into a currency format.
119
* @param float $number A floating point number
120
* @param integer $options if int then places, if string then before, if (,.-) then use it
121
* or array with places and before keys
122
* @return string formatted number
124
* @link http://book.cakephp.org/view/1457/format
126
function format($number, $options = false) {
128
if (is_int($options)) {
132
$separators = array(',', '.', '-', ':');
134
$before = $after = null;
135
if (is_string($options) && !in_array($options, $separators)) {
139
if (!is_array($options) && in_array($options, $separators)) {
140
$thousands = $options;
143
if (!is_array($options) && in_array($options, $separators)) {
144
$decimals = $options;
148
if (is_array($options)) {
149
$options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
153
$out = $before . number_format($number, $places, $decimals, $thousands) . $after;
162
* Formats a number into a currency format.
166
* - `before` - The currency symbol to place before whole numbers ie. '$'
167
* - `after` - The currency symbol to place after decimal numbers ie. 'c'. Set to boolean false to
168
* use no decimal symbol. eg. 0.35 => $0.35.
169
* - `zero` - The text to use for zero values, can be a string or a number. ie. 0, 'Free!'
170
* - `places` - Number of decimal places to use. ie. 2
171
* - `thousands` - Thousands separator ie. ','
172
* - `decimals` - Decimal separator symbol ie. '.'
173
* - `negative` - Symbol for negative numbers. If equal to '()', the number will be wrapped with ( and )
174
* - `escape` - Should the output be htmlentity escaped? Defaults to true
176
* @param float $number
177
* @param string $currency Shortcut to default options. Valid values are 'USD', 'EUR', 'GBP', otherwise
178
* set at least 'before' and 'after' options.
179
* @param array $options
180
* @return string Number formatted as a currency.
182
* @link http://book.cakephp.org/view/1453/currency
184
function currency($number, $currency = 'USD', $options = array()) {
185
$default = $this->_currencyDefaults;
187
if (isset($this->_currencies[$currency])) {
188
$default = $this->_currencies[$currency];
189
} elseif (is_string($currency)) {
190
$options['before'] = $currency;
193
$options = array_merge($default, $options);
198
if ($options['zero'] !== 0 ) {
199
return $options['zero'];
201
$options['after'] = null;
202
} elseif ($number < 1 && $number > -1 ) {
203
if ($options['after'] !== false) {
204
$multiply = intval('1' . str_pad('', $options['places'], '0'));
205
$number = $number * $multiply;
206
$options['before'] = null;
207
$options['places'] = null;
209
} elseif (empty($options['before'])) {
210
$options['before'] = null;
212
$options['after'] = null;
216
$result = $this->format($abs, $options);
219
if ($options['negative'] == '()') {
220
$result = '(' . $result .')';
222
$result = $options['negative'] . $result;
229
* Add a currency format to the Number helper. Makes reusing
230
* currency formats easier.
232
* {{{ $number->addFormat('NOK', array('before' => 'Kr. ')); }}}
234
* You can now use `NOK` as a shortform when formatting currency amounts.
236
* {{{ $number->currency($value, 'NOK'); }}}
238
* Added formats are merged with the following defaults.
242
* 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
243
* 'decimals' => '.', 'negative' => '()', 'escape' => true
247
* @param string $formatName The format name to be used in the future.
248
* @param array $options The array of options for this format.
250
* @see NumberHelper::currency()
253
function addFormat($formatName, $options) {
254
$this->_currencies[$formatName] = $options + $this->_currencyDefaults;