Changeset 1763 for branches/3.0

Show
Ignore:
Timestamp:
03/20/2007 10:21:11 PM (21 months ago)
Author:
wei
Message:

Fixed #573 in prado 3.0

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/3.0/framework/I18N/core/NumberFormat.php

    r1397 r1763  
    3232/** 
    3333 * NumberFormat class. 
    34  *  
     34 * 
    3535 * NumberFormat formats decimal numbers in any locale. The decimal 
    3636 * number is formatted according to a particular pattern. These 
     
    4141 * <code> 
    4242 *  //create a invariant number formatter. 
    43  *      $formatter = new NumberFormat();  
    44  *  
     43 *      $formatter = new NumberFormat(); 
     44 * 
    4545 *  //create a number format for the french language locale. 
    46  *  $fr = new NumberFormat('fr');  
     46 *  $fr = new NumberFormat('fr'); 
    4747 * 
    4848 *  //create a number format base on a NumberFormatInfo instance $numberInfo. 
     
    5050 * </code> 
    5151 * 
    52  * A normal decimal number can also be displayed as a currency  
     52 * A normal decimal number can also be displayed as a currency 
    5353 * or as a percentage. For example 
    5454 * <code> 
     
    6262 * <code> 
    6363 *  $ja = new NumberFormat('ja_JP'); 
    64  *  
     64 * 
    6565 *  //Japanese currency pattern, and using Japanese Yen symbol 
    6666 *  $ja->format(123.14,'c','JPY'); //ᅵ?123 (Yen 123) 
    6767 * </code> 
    6868 * For each culture, the symbol for each currency may be different. 
    69  *  
     69 * 
    7070 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com> 
    7171 * @version v1.0, last update on Fri Dec 10 18:10:20 EST 2004 
     
    7474class NumberFormat 
    7575{ 
    76          
     76 
    7777        /** 
    7878         * The DateTimeFormatInfo, containing culture specific patterns and names. 
    79          * @var DateTimeFormatInfo        
     79         * @var DateTimeFormatInfo 
    8080         */ 
    8181        protected $formatInfo; 
    82                  
     82 
    8383        /** 
    8484         * Create a new number format instance. The constructor can be instantiated 
    8585         * with a string that represent a culture/locale. Similarly, passing 
    8686         * a CultureInfo or NumberFormatInfo instance will instantiated a instance 
    87          * for that particular culture.  
     87         * for that particular culture. 
    8888         * @param mixed either null, a CultureInfo, a NumberFormatInfo, or string 
    89          * @return NumberFormat  
     89         * @return NumberFormat 
    9090         */ 
    9191        function __construct($formatInfo=null) 
     
    9898                        $this->formatInfo = $formatInfo; 
    9999                else 
    100                         $this->formatInfo =  
     100                        $this->formatInfo = 
    101101                                NumberFormatInfo::getInstance($formatInfo); 
    102102        } 
    103          
     103 
    104104        /** 
    105105         * For the number for a certain pattern. The valid patterns are 
     
    108108         * @param mixed the number to format. 
    109109         * @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 
    111111         * 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 
    113113         * "USD" represents the US Dollar and "EUR" represents the Euro currency. 
    114          * @return string formatted number string  
     114         * @return string formatted number string 
    115115         */ 
    116116        function format($number, $pattern='d', $currency='USD', $charset='UTF-8') 
    117117        { 
    118118                $this->setPattern($pattern); 
    119                                  
     119 
    120120                if(strtolower($pattern) == 'p') 
    121121                        $number = $number * 100; 
    122122 
    123123                $string = (string)$number; 
    124                  
    125                 $decimal = $this->formatDecimal($string);        
     124 
     125                $decimal = $this->formatDecimal($string); 
    126126                $integer = $this->formatInteger(abs($number)); 
    127                          
     127 
    128128                if(strlen($decimal)>0) 
    129129                        $result = $integer.$decimal; 
     
    137137                        $suffix = $this->formatInfo->NegativePattern; 
    138138                else 
    139                         $suffix = array("","");          
    140                  
     139                        $suffix = array("",""); 
     140 
    141141                //append and prepend suffix 
    142142                $result = $suffix[0].$result.$suffix[1]; 
    143                  
     143 
    144144                //replace currency sign 
    145                 $symbol = @$this->formatInfo->getCurrencySymbol($currency);      
     145                $symbol = @$this->formatInfo->getCurrencySymbol($currency); 
    146146                if(is_null($symbol)) 
    147147                        $symbol = $currency; 
    148148 
    149149                $result = str_replace('€',$symbol, $result); 
    150                  
     150 
    151151                return I18N_toEncoding($result, $charset); 
    152152        } 
    153                  
     153 
    154154        /** 
    155155         * For the integer, perform groupings and string padding. 
     
    160160        { 
    161161                $string = (string)$string; 
    162                 $dp = strpos($string, '.'); 
    163                  
     162 
    164163                $decimalDigits = $this->formatInfo->DecimalDigits; 
    165 //              var_dump($decimalDigits); 
    166164                //if not decimal digits, assume 0 decimal points. 
    167165                if(is_int($decimalDigits) && $decimalDigits > 0) 
    168166                        $string = (string)round(floatval($string),$decimalDigits); 
     167                $dp = strpos($string, '.'); 
    169168                if(is_int($dp)) 
    170169                        $string = substr($string, 0, $dp); 
    171  
    172170                $integer = ''; 
    173171 
    174172                $digitSize = $this->formatInfo->getDigitSize(); 
    175                  
     173 
    176174                $string = str_pad($string, $digitSize, '0',STR_PAD_LEFT); 
    177                  
     175 
    178176                $len = strlen($string); 
    179                  
     177 
    180178                $groupSeparator = $this->formatInfo->GroupSeparator; 
    181179                $groupSize = $this->formatInfo->GroupSizes; 
    182                  
    183                  
     180 
     181 
    184182                $firstGroup = true; 
    185183                $multiGroup = is_int($groupSize[1]); 
    186184                $count = 0; 
    187                  
     185 
    188186                if(is_int($groupSize[0])) 
    189187                { 
     
    192190                        { 
    193191                                $char = $string{$len-$i-1}; 
    194                                  
     192 
    195193                                if($multiGroup && $count == 0) 
    196194                                { 
     
    217215                                        } 
    218216                                } 
    219                                  
     217 
    220218                                $integer = $char . $integer; 
    221219                        } 
     
    223221                else 
    224222                        $integer = $string; 
    225                  
    226                 return $integer;                 
    227         } 
    228          
     223 
     224                return $integer; 
     225        } 
     226 
    229227        /** 
    230228         * Format the decimal places. 
    231229         * @param string the decimal number in string form. 
    232          * @return string formatted decimal places.  
     230         * @return string formatted decimal places. 
    233231         */ 
    234232        protected function formatDecimal($string) 
    235         {                
     233        { 
    236234                $dp = strpos($string, '.'); 
    237235                $decimal = ''; 
    238          
     236 
    239237                $decimalDigits = $this->formatInfo->DecimalDigits; 
    240238                $decimalSeparator = $this->formatInfo->DecimalSeparator; 
    241                  
     239 
    242240                //do the correct rounding here 
    243241                //$string = round(floatval($string), $decimalDigits); 
    244242                if(is_int($dp)) 
    245                 {                
     243                { 
    246244                        if($decimalDigits == -1) 
    247245                        { 
    248246                                $decimal = substr($string, $dp+1); 
    249                         }                        
     247                        } 
    250248                        else if(is_int($decimalDigits)) 
    251                         {                                
     249                        { 
    252250                                $float = round((float)$string, $decimalDigits); 
    253251                                if(strpos((string)$float, '.') === false) 
     
    262260                                } 
    263261                        } 
    264                         else  
     262                        else 
    265263                                return $decimal; 
    266                          
    267                         return $decimalSeparator.$decimal;               
     264 
     265                        return $decimalSeparator.$decimal; 
    268266                } 
    269267                else if ($decimalDigits > 0) 
    270                         return $decimalSeparator.str_pad($decimal,$decimalDigits,'0');                   
    271                  
     268                        return $decimalSeparator.str_pad($decimal,$decimalDigits,'0'); 
     269 
    272270                return $decimal; 
    273271        } 
    274          
     272 
    275273        /** 
    276274         * Set the pattern to format against. The default patterns