Changeset 1763 for branches/3.0
- Timestamp:
- 03/20/2007 10:21:11 PM (21 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/framework/I18N/core/NumberFormat.php
r1397 r1763 32 32 /** 33 33 * NumberFormat class. 34 * 34 * 35 35 * NumberFormat formats decimal numbers in any locale. The decimal 36 36 * number is formatted according to a particular pattern. These … … 41 41 * <code> 42 42 * //create a invariant number formatter. 43 * $formatter = new NumberFormat(); 44 * 43 * $formatter = new NumberFormat(); 44 * 45 45 * //create a number format for the french language locale. 46 * $fr = new NumberFormat('fr'); 46 * $fr = new NumberFormat('fr'); 47 47 * 48 48 * //create a number format base on a NumberFormatInfo instance $numberInfo. … … 50 50 * </code> 51 51 * 52 * A normal decimal number can also be displayed as a currency 52 * A normal decimal number can also be displayed as a currency 53 53 * or as a percentage. For example 54 54 * <code> … … 62 62 * <code> 63 63 * $ja = new NumberFormat('ja_JP'); 64 * 64 * 65 65 * //Japanese currency pattern, and using Japanese Yen symbol 66 66 * $ja->format(123.14,'c','JPY'); //ᅵ?123 (Yen 123) 67 67 * </code> 68 68 * For each culture, the symbol for each currency may be different. 69 * 69 * 70 70 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 71 71 * @version v1.0, last update on Fri Dec 10 18:10:20 EST 2004 … … 74 74 class NumberFormat 75 75 { 76 76 77 77 /** 78 78 * The DateTimeFormatInfo, containing culture specific patterns and names. 79 * @var DateTimeFormatInfo 79 * @var DateTimeFormatInfo 80 80 */ 81 81 protected $formatInfo; 82 82 83 83 /** 84 84 * Create a new number format instance. The constructor can be instantiated 85 85 * with a string that represent a culture/locale. Similarly, passing 86 86 * a CultureInfo or NumberFormatInfo instance will instantiated a instance 87 * for that particular culture. 87 * for that particular culture. 88 88 * @param mixed either null, a CultureInfo, a NumberFormatInfo, or string 89 * @return NumberFormat 89 * @return NumberFormat 90 90 */ 91 91 function __construct($formatInfo=null) … … 98 98 $this->formatInfo = $formatInfo; 99 99 else 100 $this->formatInfo = 100 $this->formatInfo = 101 101 NumberFormatInfo::getInstance($formatInfo); 102 102 } 103 103 104 104 /** 105 105 * For the number for a certain pattern. The valid patterns are … … 108 108 * @param mixed the number to format. 109 109 * @param string the format pattern, either, 'c', 'd', 'e', 'p' 110 * or a custom pattern. E.g. "#.000" will format the number to 110 * or a custom pattern. E.g. "#.000" will format the number to 111 111 * 3 decimal places. 112 * @param string 3-letter ISO 4217 code. For example, the code 112 * @param string 3-letter ISO 4217 code. For example, the code 113 113 * "USD" represents the US Dollar and "EUR" represents the Euro currency. 114 * @return string formatted number string 114 * @return string formatted number string 115 115 */ 116 116 function format($number, $pattern='d', $currency='USD', $charset='UTF-8') 117 117 { 118 118 $this->setPattern($pattern); 119 119 120 120 if(strtolower($pattern) == 'p') 121 121 $number = $number * 100; 122 122 123 123 $string = (string)$number; 124 125 $decimal = $this->formatDecimal($string); 124 125 $decimal = $this->formatDecimal($string); 126 126 $integer = $this->formatInteger(abs($number)); 127 127 128 128 if(strlen($decimal)>0) 129 129 $result = $integer.$decimal; … … 137 137 $suffix = $this->formatInfo->NegativePattern; 138 138 else 139 $suffix = array("",""); 140 139 $suffix = array("",""); 140 141 141 //append and prepend suffix 142 142 $result = $suffix[0].$result.$suffix[1]; 143 143 144 144 //replace currency sign 145 $symbol = @$this->formatInfo->getCurrencySymbol($currency); 145 $symbol = @$this->formatInfo->getCurrencySymbol($currency); 146 146 if(is_null($symbol)) 147 147 $symbol = $currency; 148 148 149 149 $result = str_replace('€',$symbol, $result); 150 150 151 151 return I18N_toEncoding($result, $charset); 152 152 } 153 153 154 154 /** 155 155 * For the integer, perform groupings and string padding. … … 160 160 { 161 161 $string = (string)$string; 162 $dp = strpos($string, '.'); 163 162 164 163 $decimalDigits = $this->formatInfo->DecimalDigits; 165 // var_dump($decimalDigits);166 164 //if not decimal digits, assume 0 decimal points. 167 165 if(is_int($decimalDigits) && $decimalDigits > 0) 168 166 $string = (string)round(floatval($string),$decimalDigits); 167 $dp = strpos($string, '.'); 169 168 if(is_int($dp)) 170 169 $string = substr($string, 0, $dp); 171 172 170 $integer = ''; 173 171 174 172 $digitSize = $this->formatInfo->getDigitSize(); 175 173 176 174 $string = str_pad($string, $digitSize, '0',STR_PAD_LEFT); 177 175 178 176 $len = strlen($string); 179 177 180 178 $groupSeparator = $this->formatInfo->GroupSeparator; 181 179 $groupSize = $this->formatInfo->GroupSizes; 182 183 180 181 184 182 $firstGroup = true; 185 183 $multiGroup = is_int($groupSize[1]); 186 184 $count = 0; 187 185 188 186 if(is_int($groupSize[0])) 189 187 { … … 192 190 { 193 191 $char = $string{$len-$i-1}; 194 192 195 193 if($multiGroup && $count == 0) 196 194 { … … 217 215 } 218 216 } 219 217 220 218 $integer = $char . $integer; 221 219 } … … 223 221 else 224 222 $integer = $string; 225 226 return $integer; 227 } 228 223 224 return $integer; 225 } 226 229 227 /** 230 228 * Format the decimal places. 231 229 * @param string the decimal number in string form. 232 * @return string formatted decimal places. 230 * @return string formatted decimal places. 233 231 */ 234 232 protected function formatDecimal($string) 235 { 233 { 236 234 $dp = strpos($string, '.'); 237 235 $decimal = ''; 238 236 239 237 $decimalDigits = $this->formatInfo->DecimalDigits; 240 238 $decimalSeparator = $this->formatInfo->DecimalSeparator; 241 239 242 240 //do the correct rounding here 243 241 //$string = round(floatval($string), $decimalDigits); 244 242 if(is_int($dp)) 245 { 243 { 246 244 if($decimalDigits == -1) 247 245 { 248 246 $decimal = substr($string, $dp+1); 249 } 247 } 250 248 else if(is_int($decimalDigits)) 251 { 249 { 252 250 $float = round((float)$string, $decimalDigits); 253 251 if(strpos((string)$float, '.') === false) … … 262 260 } 263 261 } 264 else 262 else 265 263 return $decimal; 266 267 return $decimalSeparator.$decimal; 264 265 return $decimalSeparator.$decimal; 268 266 } 269 267 else if ($decimalDigits > 0) 270 return $decimalSeparator.str_pad($decimal,$decimalDigits,'0'); 271 268 return $decimalSeparator.str_pad($decimal,$decimalDigits,'0'); 269 272 270 return $decimal; 273 271 } 274 272 275 273 /** 276 274 * Set the pattern to format against. The default patterns
