Package | system.i18n |
---|---|
Inheritance | class CNumberFormatter » CComponent |
Since | 1.0 |
Version | $Id$ |
Source Code | framework/i18n/CNumberFormatter.php |
Pattern "#,##0.00" will format 12345.678 as "12,345.68". Pattern "#,#,#0.00" will format 12345.6 as "1,2,3,45.60".Note, in the first example, the number is rounded first before applying the formatting. And in the second example, the pattern specifies two grouping sizes.
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CNumberFormatter |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
format() | Formats a number based on the specified pattern. | CNumberFormatter |
formatCurrency() | Formats a number using the currency format defined in the locale. | CNumberFormatter |
formatDecimal() | Formats a number using the decimal format defined in the locale. | CNumberFormatter |
formatPercentage() | Formats a number using the percentage format defined in the locale. | CNumberFormatter |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
raiseEvent() | Raises an event. | CComponent |
Method | Description | Defined By |
---|---|---|
formatNumber() | Formats a number based on a format. | CNumberFormatter |
parseFormat() | Parses a given string pattern. | CNumberFormatter |
public void __construct(mixed $locale)
| ||
$locale | mixed | locale ID (string) or CLocale instance |
public function __construct($locale)
{
if(is_string($locale))
$this->_locale=CLocale::getInstance($locale);
else
$this->_locale=$locale;
}
Constructor.
public string format(string $pattern, mixed $value, string $currency=NULL)
| ||
$pattern | string | format pattern |
$value | mixed | the number to be formatted |
$currency | string | 3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency. The currency placeholder in the pattern will be replaced with the currency symbol. If null, no replacement will be done. |
{return} | string | the formatting result. |
public function format($pattern,$value,$currency=null)
{
$format=$this->parseFormat($pattern);
$result=$this->formatNumber($format,$value);
if($currency===null)
return $result;
else if(($symbol=$this->_locale->getCurrencySymbol($currency))===null)
$symbol=$currency;
return str_replace('¤',$symbol,$result);
}
Formats a number based on the specified pattern. Note, if the format contains '%', the number will be multiplied by 100 first. If the format contains '‰', the number will be multiplied by 1000. If the format contains currency placeholder, it will be replaced by the specified localized currency symbol.
public string formatCurrency(mixed $value, string $currency)
| ||
$value | mixed | the number to be formatted |
$currency | string | 3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency. The currency placeholder in the pattern will be replaced with the currency symbol. |
{return} | string | the formatting result. |
public function formatCurrency($value,$currency)
{
return $this->format($this->_locale->getCurrencyFormat(),$value,$currency);
}
Formats a number using the currency format defined in the locale.
public string formatDecimal(mixed $value)
| ||
$value | mixed | the number to be formatted |
{return} | string | the formatting result. |
public function formatDecimal($value)
{
return $this->format($this->_locale->getDecimalFormat(),$value);
}
Formats a number using the decimal format defined in the locale.
protected string formatNumber(array $format, mixed $value)
| ||
$format | array | format with the following structure:
array( 'decimalDigits'=>2, // number of required digits after decimal point; 0s will be padded if not enough digits; if -1, it means we should drop decimal point 'maxDecimalDigits'=>3, // maximum number of digits after decimal point. Additional digits will be truncated. 'integerDigits'=>1, // number of required digits before decimal point; 0s will be padded if not enough digits 'groupSize1'=>3, // the primary grouping size; if 0, it means no grouping 'groupSize2'=>0, // the secondary grouping size; if 0, it means no secondary grouping 'positivePrefix'=>'+', // prefix to positive number 'positiveSuffix'=>'', // suffix to positive number 'negativePrefix'=>'(', // prefix to negative number 'negativeSuffix'=>')', // suffix to negative number 'multiplier'=>1, // 100 for percent, 1000 for per mille ); |
$value | mixed | the number to be formatted |
{return} | string | the formatted result |
protected function formatNumber($format,$value)
{
$negative=$value<0;
$value=abs($value*$format['multiplier']);
if($format['maxDecimalDigits']>=0)
$value=round($value,$format['maxDecimalDigits']);
$value="$value";
if(($pos=strpos($value,'.'))!==false)
{
$integer=substr($value,0,$pos);
$decimal=substr($value,$pos+1);
}
else
{
$integer=$value;
$decimal='';
}
if($format['decimalDigits']>strlen($decimal))
$decimal=str_pad($decimal,$format['decimalDigits'],'0');
if(strlen($decimal)>0)
$decimal=$this->_locale->getNumberSymbol('decimal').$decimal;
$integer=str_pad($integer,$format['integerDigits'],'0',STR_PAD_LEFT);
if($format['groupSize1']>0 && strlen($integer)>$format['groupSize1'])
{
$str1=substr($integer,0,-$format['groupSize1']);
$str2=substr($integer,-$format['groupSize1']);
$size=$format['groupSize2']>0?$format['groupSize2']:$format['groupSize1'];
$str1=str_pad($str1,(int)((strlen($str1)+$size-1)/$size)*$size,' ',STR_PAD_LEFT);
$integer=ltrim(implode($this->_locale->getNumberSymbol('group'),str_split($str1,$size))).$this->_locale->getNumberSymbol('group').$str2;
}
if($negative)
$number=$format['negativePrefix'].$integer.$decimal.$format['negativeSuffix'];
else
$number=$format['positivePrefix'].$integer.$decimal.$format['positiveSuffix'];
return strtr($number,array('%'=>$this->_locale->getNumberSymbol('percentSign'),'‰'=>$this->_locale->getNumberSymbol('perMille')));
}
Formats a number based on a format. This is the method that does actual number formatting.
public string formatPercentage(mixed $value)
| ||
$value | mixed | the number to be formatted |
{return} | string | the formatting result. |
public function formatPercentage($value)
{
return $this->format($this->_locale->getPercentFormat(),$value);
}
Formats a number using the percentage format defined in the locale. Note, if the percentage format contains '%', the number will be multiplied by 100 first. If the percentage format contains '‰', the number will be multiplied by 1000.
protected array parseFormat(string $pattern)
| ||
$pattern | string | the pattern to be parsed |
{return} | array | the parsed pattern |
protected function parseFormat($pattern)
{
if(isset($this->_formats[$pattern]))
return $this->_formats[$pattern];
$format=array();
// find out prefix and suffix for positive and negative patterns
$patterns=explode(';',$pattern);
$format['positivePrefix']=$format['positiveSuffix']=$format['negativePrefix']=$format['negativeSuffix']='';
if(preg_match('/^(.*?)[#,\.0]+(.*?)$/',$patterns[0],$matches))
{
$format['positivePrefix']=$matches[1];
$format['positiveSuffix']=$matches[2];
}
if(isset($patterns[1]) && preg_match('/^(.*?)[#,\.0]+(.*?)$/',$patterns[1],$matches)) // with a negative pattern
{
$format['negativePrefix']=$matches[1];
$format['negativeSuffix']=$matches[2];
}
else
{
$format['negativePrefix']=$this->_locale->getNumberSymbol('minusSign').$format['positivePrefix'];
$format['negativeSuffix']=$format['positiveSuffix'];
}
$pat=$patterns[0];
// find out multiplier
if(strpos($pat,'%')!==false)
$format['multiplier']=100;
else if(strpos($pat,'‰')!==false)
$format['multiplier']=1000;
else
$format['multiplier']=1;
// find out things about decimal part
if(($pos=strpos($pat,'.'))!==false)
{
if(($pos2=strrpos($pat,'0'))>$pos)
$format['decimalDigits']=$pos2-$pos;
else
$format['decimalDigits']=0;
if(($pos3=strrpos($pat,'#'))>=$pos2)
$format['maxDecimalDigits']=$pos3-$pos;
else
$format['maxDecimalDigits']=$format['decimalDigits'];
$pat=substr($pat,0,$pos);
}
else // no decimal part
{
$format['decimalDigits']=0;
$format['maxDecimalDigits']=0;
}
// find out things about integer part
$p=str_replace(',','',$pat);
if(($pos=strpos($p,'0'))!==false)
$format['integerDigits']=strrpos($p,'0')-$pos+1;
else
$format['integerDigits']=0;
// find out group sizes. some patterns may have two different group sizes
$p=str_replace('#','0',$pat);
if(($pos=strrpos($pat,','))!==false)
{
$format['groupSize1']=strrpos($p,'0')-$pos;
if(($pos2=strrpos(substr($p,0,$pos),','))!==false)
$format['groupSize2']=$pos-$pos2-1;
else
$format['groupSize2']=0;
}
else
$format['groupSize1']=$format['groupSize2']=0;
return $this->_formats[$pattern]=$format;
}
Parses a given string pattern.
Signup or Login in order to comment.