summaryrefslogtreecommitdiff
path: root/framework/I18N
diff options
context:
space:
mode:
authorwei <>2007-03-21 03:18:54 +0000
committerwei <>2007-03-21 03:18:54 +0000
commit4b41982314af49396ee577c8d4dea0624b8e9096 (patch)
treeaff9f16e87c2210bba740b6a7a6108c1629034cf /framework/I18N
parent8dbd201ad9a6945ba00a29c203b6c9c2c0e1ca53 (diff)
Fixed #573
Diffstat (limited to 'framework/I18N')
-rw-r--r--framework/I18N/core/NumberFormat.php108
1 files changed, 53 insertions, 55 deletions
diff --git a/framework/I18N/core/NumberFormat.php b/framework/I18N/core/NumberFormat.php
index b30e615b..24869b3f 100644
--- a/framework/I18N/core/NumberFormat.php
+++ b/framework/I18N/core/NumberFormat.php
@@ -31,7 +31,7 @@ require_once(dirname(__FILE__).'/util.php');
/**
* NumberFormat class.
- *
+ *
* NumberFormat formats decimal numbers in any locale. The decimal
* number is formatted according to a particular pattern. These
* patterns can arise from the NumberFormatInfo object which is
@@ -40,16 +40,16 @@ require_once(dirname(__FILE__).'/util.php');
*
* <code>
* //create a invariant number formatter.
- * $formatter = new NumberFormat();
- *
+ * $formatter = new NumberFormat();
+ *
* //create a number format for the french language locale.
- * $fr = new NumberFormat('fr');
+ * $fr = new NumberFormat('fr');
*
* //create a number format base on a NumberFormatInfo instance $numberInfo.
* $format = new NumberFormat($numberInfo);
* </code>
*
- * A normal decimal number can also be displayed as a currency
+ * A normal decimal number can also be displayed as a currency
* or as a percentage. For example
* <code>
* $format->format(1234.5); //Decimal number "1234.5"
@@ -61,32 +61,32 @@ require_once(dirname(__FILE__).'/util.php');
* to format the number as Japanese Yen:
* <code>
* $ja = new NumberFormat('ja_JP');
- *
+ *
* //Japanese currency pattern, and using Japanese Yen symbol
* $ja->format(123.14,'c','JPY'); //�?123 (Yen 123)
* </code>
* For each culture, the symbol for each currency may be different.
- *
+ *
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
* @version v1.0, last update on Fri Dec 10 18:10:20 EST 2004
* @package System.I18N.core
*/
class NumberFormat
{
-
+
/**
* The DateTimeFormatInfo, containing culture specific patterns and names.
- * @var DateTimeFormatInfo
+ * @var DateTimeFormatInfo
*/
protected $formatInfo;
-
+
/**
* Create a new number format instance. The constructor can be instantiated
* with a string that represent a culture/locale. Similarly, passing
* a CultureInfo or NumberFormatInfo instance will instantiated a instance
- * for that particular culture.
+ * for that particular culture.
* @param mixed either null, a CultureInfo, a NumberFormatInfo, or string
- * @return NumberFormat
+ * @return NumberFormat
*/
function __construct($formatInfo=null)
{
@@ -97,34 +97,34 @@ class NumberFormat
else if($formatInfo instanceof NumberFormatInfo)
$this->formatInfo = $formatInfo;
else
- $this->formatInfo =
+ $this->formatInfo =
NumberFormatInfo::getInstance($formatInfo);
}
-
+
/**
* For the number for a certain pattern. The valid patterns are
* 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for
* 3 decimal places.
* @param mixed the number to format.
* @param string the format pattern, either, 'c', 'd', 'e', 'p'
- * or a custom pattern. E.g. "#.000" will format the number to
+ * or a custom pattern. E.g. "#.000" will format the number to
* 3 decimal places.
- * @param string 3-letter ISO 4217 code. For example, the code
+ * @param string 3-letter ISO 4217 code. For example, the code
* "USD" represents the US Dollar and "EUR" represents the Euro currency.
- * @return string formatted number string
+ * @return string formatted number string
*/
function format($number, $pattern='d', $currency='USD', $charset='UTF-8')
{
$this->setPattern($pattern);
-
+
if(strtolower($pattern) == 'p')
$number = $number * 100;
$string = (string)$number;
-
- $decimal = $this->formatDecimal($string);
+
+ $decimal = $this->formatDecimal($string);
$integer = $this->formatInteger(abs($number));
-
+
if(strlen($decimal)>0)
$result = $integer.$decimal;
else
@@ -136,21 +136,21 @@ class NumberFormat
else if($number < 0)
$suffix = $this->formatInfo->NegativePattern;
else
- $suffix = array("","");
-
+ $suffix = array("","");
+
//append and prepend suffix
$result = $suffix[0].$result.$suffix[1];
-
+
//replace currency sign
- $symbol = @$this->formatInfo->getCurrencySymbol($currency);
+ $symbol = @$this->formatInfo->getCurrencySymbol($currency);
if(is_null($symbol))
$symbol = $currency;
$result = str_replace('¤',$symbol, $result);
-
+
return I18N_toEncoding($result, $charset);
}
-
+
/**
* For the integer, perform groupings and string padding.
* @param string the decimal number in string form.
@@ -159,39 +159,37 @@ class NumberFormat
protected function formatInteger($string)
{
$string = (string)$string;
- $dp = strpos($string, '.');
-
+
$decimalDigits = $this->formatInfo->DecimalDigits;
-// var_dump($decimalDigits);
//if not decimal digits, assume 0 decimal points.
if(is_int($decimalDigits) && $decimalDigits > 0)
$string = (string)round(floatval($string),$decimalDigits);
+ $dp = strpos($string, '.');
if(is_int($dp))
$string = substr($string, 0, $dp);
-
$integer = '';
$digitSize = $this->formatInfo->getDigitSize();
-
+
$string = str_pad($string, $digitSize, '0',STR_PAD_LEFT);
-
+
$len = strlen($string);
-
+
$groupSeparator = $this->formatInfo->GroupSeparator;
$groupSize = $this->formatInfo->GroupSizes;
-
-
+
+
$firstGroup = true;
$multiGroup = is_int($groupSize[1]);
$count = 0;
-
+
if(is_int($groupSize[0]))
{
//now for the integer groupings
for($i=0; $i<$len; $i++)
{
$char = $string{$len-$i-1};
-
+
if($multiGroup && $count == 0)
{
if($i != 0 && $i%$groupSize[0] == 0)
@@ -216,39 +214,39 @@ class NumberFormat
$count++;
}
}
-
+
$integer = $char . $integer;
}
}
else
$integer = $string;
-
- return $integer;
+
+ return $integer;
}
-
+
/**
* Format the decimal places.
* @param string the decimal number in string form.
- * @return string formatted decimal places.
+ * @return string formatted decimal places.
*/
protected function formatDecimal($string)
- {
+ {
$dp = strpos($string, '.');
$decimal = '';
-
+
$decimalDigits = $this->formatInfo->DecimalDigits;
$decimalSeparator = $this->formatInfo->DecimalSeparator;
-
+
//do the correct rounding here
//$string = round(floatval($string), $decimalDigits);
if(is_int($dp))
- {
+ {
if($decimalDigits == -1)
{
$decimal = substr($string, $dp+1);
- }
+ }
else if(is_int($decimalDigits))
- {
+ {
$float = round((float)$string, $decimalDigits);
if(strpos((string)$float, '.') === false)
{
@@ -261,17 +259,17 @@ class NumberFormat
$decimal = str_pad($decimal,$decimalDigits,'0');
}
}
- else
+ else
return $decimal;
-
- return $decimalSeparator.$decimal;
+
+ return $decimalSeparator.$decimal;
}
else if ($decimalDigits > 0)
- return $decimalSeparator.str_pad($decimal,$decimalDigits,'0');
-
+ return $decimalSeparator.str_pad($decimal,$decimalDigits,'0');
+
return $decimal;
}
-
+
/**
* Set the pattern to format against. The default patterns
* are retrieved from the NumberFormatInfo instance.