[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /** 4 * sfNumberFormat class file. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the BSD License. 8 * 9 * Copyright(c) 2004 by Qiang Xue. All rights reserved. 10 * 11 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue} 12 * The latest version of PRADO can be obtained from: 13 * {@link http://prado.sourceforge.net/} 14 * 15 * @author Wei Zhuo <weizhuo[at]gmail[dot]com> 16 * @version $Id: sfNumberFormat.class.php 2834 2006-11-27 14:09:05Z fabien $ 17 * @package symfony 18 * @subpackage i18n 19 */ 20 21 /** 22 * Get the encoding utilities 23 */ 24 require_once(dirname(__FILE__).'/util.php'); 25 26 /** 27 * sfNumberFormat class. 28 * 29 * sfNumberFormat formats decimal numbers in any locale. The decimal 30 * number is formatted according to a particular pattern. These 31 * patterns can arise from the sfNumberFormatInfo object which is 32 * culturally sensitive. The sfNumberFormat class can be instantiated in 33 * many ways. E.g. 34 * 35 * <code> 36 * //create a invariant number formatter. 37 * $formatter = new sfNumberFormat(); 38 * 39 * //create a number format for the french language locale. 40 * $fr = new sfNumberFormat('fr'); 41 * 42 * //create a number format base on a sfNumberFormatInfo instance $numberInfo. 43 * $format = new sfNumberFormat($numberInfo); 44 * </code> 45 * 46 * A normal decimal number can also be displayed as a currency 47 * or as a percentage. For example 48 * <code> 49 * $format->format(1234.5); //Decimal number "1234.5" 50 * $format->format(1234.5,'c'); //Default currency "$1234.50" 51 * $format->format(0.25, 'p') //Percent "25%" 52 * </code> 53 * 54 * Currency is formated using the localized currency pattern. For example 55 * to format the number as Japanese Yen: 56 * <code> 57 * $ja = new sfNumberFormat('ja_JP'); 58 * 59 * //Japanese currency pattern, and using Japanese Yen symbol 60 * $ja->format(123.14,'c','JPY'); //ï¿?123 (Yen 123) 61 * </code> 62 * For each culture, the symbol for each currency may be different. 63 * 64 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 65 * @version v1.0, last update on Fri Dec 10 18:10:20 EST 2004 66 * @package System.I18N.core 67 */ 68 class sfNumberFormat 69 { 70 /** 71 * The DateTimeFormatInfo, containing culture specific patterns and names. 72 * @var DateTimeFormatInfo 73 */ 74 protected $formatInfo; 75 76 /** 77 * Create a new number format instance. The constructor can be instantiated 78 * with a string that represent a culture/locale. Similarly, passing 79 * a sfCultureInfo or sfNumberFormatInfo instance will instantiated a instance 80 * for that particular culture. 81 * 82 * @param mixed either null, a sfCultureInfo, a sfNumberFormatInfo, or string 83 * @return sfNumberFormat 84 */ 85 function __construct($formatInfo = null) 86 { 87 if (is_null($formatInfo)) 88 { 89 $this->formatInfo = sfNumberFormatInfo::getInvariantInfo(); 90 } 91 else if ($formatInfo instanceof sfCultureInfo) 92 { 93 $this->formatInfo = $formatInfo->sfNumberFormat; 94 } 95 else if ($formatInfo instanceof sfNumberFormatInfo) 96 { 97 $this->formatInfo = $formatInfo; 98 } 99 else 100 { 101 $this->formatInfo = sfNumberFormatInfo::getInstance($formatInfo); 102 } 103 } 104 105 /** 106 * For the number for a certain pattern. The valid patterns are 107 * 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for 108 * 3 decimal places. 109 * 110 * @param mixed the number to format. 111 * @param string the format pattern, either, 'c', 'd', 'e', 'p' 112 * or a custom pattern. E.g. "#.000" will format the number to 113 * 3 decimal places. 114 * @param string 3-letter ISO 4217 code. For example, the code 115 * "USD" represents the US Dollar and "EUR" represents the Euro currency. 116 * @return string formatted number string 117 */ 118 function format($number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8') 119 { 120 $this->setPattern($pattern); 121 122 if (strtolower($pattern) == 'p') 123 { 124 $number = $number * 100; 125 } 126 127 $string = (string) $number; 128 129 list($number, $decimal) = $this->formatDecimal($string); 130 $integer = $this->formatInteger(abs($number)); 131 132 $result = (strlen($decimal) > 0) ? $integer.$decimal : $integer; 133 134 // get the suffix 135 if ($number >= 0) 136 { 137 $suffix = $this->formatInfo->PositivePattern; 138 } 139 else if ($number < 0) 140 { 141 $suffix = $this->formatInfo->NegativePattern; 142 } 143 else 144 { 145 $suffix = array('', ''); 146 } 147 148 // append and prepend suffix 149 $result = $suffix[0].$result.$suffix[1]; 150 151 // replace currency sign 152 $symbol = @$this->formatInfo->getCurrencySymbol($currency); 153 if (is_null($symbol)) 154 { 155 $symbol = $currency; 156 } 157 158 $result = str_replace('¤', $symbol, $result); 159 160 return I18N_toEncoding($result, $charset); 161 } 162 163 /** 164 * For the integer, perform groupings and string padding. 165 * 166 * @param string the decimal number in string form. 167 * @return string formatted integer string with grouping 168 */ 169 protected function formatInteger($string) 170 { 171 $string = (string) $string; 172 $dp = strpos($string, '.'); 173 174 if (is_int($dp)) 175 { 176 $string = substr($string, 0, $dp); 177 } 178 179 $integer = ''; 180 181 $len = strlen($string); 182 183 $groupSeparator = $this->formatInfo->GroupSeparator; 184 $groupSize = $this->formatInfo->GroupSizes; 185 186 $firstGroup = true; 187 $multiGroup = is_int($groupSize[1]); 188 $count = 0; 189 190 if (is_int($groupSize[0])) 191 { 192 // now for the integer groupings 193 for ($i = 0; $i < $len; $i++) 194 { 195 $char = $string{$len - $i - 1}; 196 197 if ($multiGroup && $count == 0) 198 { 199 if ($i != 0 && $i % $groupSize[0] == 0) 200 { 201 $integer = $groupSeparator.$integer; 202 $count++; 203 } 204 } 205 else if ($multiGroup && $count >= 1) 206 { 207 if ($i != 0 && ($i-$groupSize[0])%$groupSize[1] == 0) 208 { 209 $integer = $groupSeparator.$integer; 210 $count++; 211 } 212 } 213 else 214 { 215 if ($i != 0 && $i % $groupSize[0] == 0) 216 { 217 $integer = $groupSeparator.$integer; 218 $count++; 219 } 220 } 221 222 $integer = $char.$integer; 223 } 224 } 225 else 226 { 227 $integer = $string; 228 } 229 230 return $integer; 231 } 232 233 /** 234 * Format the decimal places. 235 * 236 * @param string the decimal number in string form. 237 * @return string formatted decimal places. 238 */ 239 protected function formatDecimal($string) 240 { 241 $dp = strpos($string, '.'); 242 $decimal = ''; 243 244 $decimalDigits = $this->formatInfo->DecimalDigits; 245 $decimalSeparator = $this->formatInfo->DecimalSeparator; 246 247 if (is_int($dp)) 248 { 249 if ($decimalDigits == -1) 250 { 251 $decimal = substr($string, $dp + 1); 252 } 253 else if (is_int($decimalDigits)) 254 { 255 $string = $float = round((float)$string, $decimalDigits); 256 if (strpos((string)$float, '.') === false) 257 { 258 $decimal = str_pad($decimal, $decimalDigits, '0'); 259 } 260 else 261 { 262 $decimal = substr($float, strpos($float,'.') + 1); 263 if (strlen($decimal)<$decimalDigits) 264 { 265 $decimal = str_pad($decimal, $decimalDigits, '0'); 266 } 267 } 268 } 269 else 270 { 271 return array($string, $decimal); 272 } 273 274 return array($string, $decimalSeparator.$decimal); 275 } 276 else if ($decimalDigits > 0) 277 { 278 return array($string, $decimalSeparator.str_pad($decimal, $decimalDigits, '0')); 279 } 280 281 return array($string, $decimal); 282 } 283 284 /** 285 * Set the pattern to format against. The default patterns 286 * are retrieved from the sfNumberFormatInfo instance. 287 * 288 * @param string the requested patterns. 289 * @return string a number format pattern. 290 */ 291 protected function setPattern($pattern) 292 { 293 switch ($pattern) 294 { 295 case 'c': 296 case 'C': 297 $this->formatInfo->setPattern(sfNumberFormatInfo::CURRENCY); 298 break; 299 case 'd': 300 case 'D': 301 $this->formatInfo->setPattern(sfNumberFormatInfo::DECIMAL); 302 break; 303 case 'e': 304 case 'E': 305 $this->formatInfo->setPattern(sfNumberFormatInfo::SCIENTIFIC); 306 break; 307 case 'p': 308 case 'P': 309 $this->formatInfo->setPattern(sfNumberFormatInfo::PERCENTAGE); 310 break; 311 default: 312 $this->formatInfo->setPattern($pattern); 313 break; 314 } 315 } 316 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |