00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 class NumberHelper extends AppHelper {
00040
00041
00042
00043
00044
00045
00046
00047
00048 function precision($number, $precision = 3) {
00049 return sprintf("%01.{$precision}f", $number);
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 function toReadableSize($size) {
00060 switch($size) {
00061 case 0:
00062 return '0 Bytes';
00063 case 1:
00064 return '1 Byte';
00065 case $size < 1024:
00066 return $size . ' Bytes';
00067 case $size < 1024 * 1024:
00068 return $this->precision($size / 1024, 0) . ' KB';
00069 case $size < 1024 * 1024 * 1024:
00070 return $this->precision($size / 1024 / 1024, 2) . ' MB';
00071 case $size < 1024 * 1024 * 1024 * 1024:
00072 return $this->precision($size / 1024 / 1024 / 1024, 2) . ' GB';
00073 case $size < 1024 * 1024 * 1024 * 1024 * 1024:
00074 return $this->precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB';
00075 }
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 function toPercentage($number, $precision = 2) {
00086 return $this->precision($number, $precision) . '%';
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 function format($number, $options = false) {
00098 $places = 0;
00099 if (is_int($options)) {
00100 $places = $options;
00101 }
00102
00103 $separators = array(',', '.', '-', ':');
00104
00105 $before = $after = null;
00106 if (is_string($options) && !in_array($options, $separators)) {
00107 $before = $options;
00108 }
00109 $thousands = ',';
00110 if (!is_array($options) && in_array($options, $separators)) {
00111 $thousands = $options;
00112 }
00113 $decimals = '.';
00114 if (!is_array($options) && in_array($options, $separators)) {
00115 $decimals = $options;
00116 }
00117
00118 $escape = true;
00119 if (is_array($options)) {
00120 $options = array_merge(array('before'=>'$', 'places' => 2, 'thousands' => ',', 'decimals' => '.'), $options);
00121 extract($options);
00122 }
00123
00124 $out = $before . number_format($number, $places, $decimals, $thousands) . $after;
00125
00126 if ($escape) {
00127 return h($out);
00128 }
00129 return $out;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 function currency($number, $currency = 'USD', $options = array()) {
00141 $default = array(
00142 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',',
00143 'decimals' => '.','negative' => '()', 'escape' => true
00144 );
00145 $currencies = array(
00146 'USD' => array(
00147 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',',
00148 'decimals' => '.', 'negative' => '()', 'escape' => true
00149 ),
00150 'GBP' => array(
00151 'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',',
00152 'decimals' => '.', 'negative' => '()','escape' => false
00153 ),
00154 'EUR' => array(
00155 'before'=>'€', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => '.',
00156 'decimals' => ',', 'negative' => '()', 'escape' => false
00157 )
00158 );
00159
00160 if (isset($currencies[$currency])) {
00161 $default = $currencies[$currency];
00162 } elseif (is_string($currency)) {
00163 $options['before'] = $currency;
00164 }
00165
00166 $options = array_merge($default, $options);
00167
00168 $result = null;
00169
00170 if ($number == 0 ) {
00171 if ($options['zero'] !== 0 ) {
00172 return $options['zero'];
00173 }
00174 $options['after'] = null;
00175 } elseif ($number < 1 && $number > -1 ) {
00176 $multiply = intval('1' . str_pad('', $options['places'], '0'));
00177 $number = $number * $multiply;
00178 $options['before'] = null;
00179 $options['places'] = null;
00180 } else {
00181 $options['after'] = null;
00182 }
00183
00184 $abs = abs($number);
00185 $result = $this->format($abs, $options);
00186
00187 if ($number < 0 ) {
00188 if($options['negative'] == '()') {
00189 $result = '(' . $result .')';
00190 } else {
00191 $result = $options['negative'] . $result;
00192 }
00193 }
00194 return $result;
00195 }
00196 }
00197
00198 ?>