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 culturally sensitive. The NumberFormat class can be instantiated in many ways. E.g. create a invariant number formatter. $formatter = new NumberFormat(); create a number format for the french language locale. $fr = new NumberFormat('fr'); create a number format base on a NumberFormatInfo instance $numberInfo. $format = new NumberFormat($numberInfo); A normal decimal number can also be displayed as a currency or as a percentage. For example $format->format(1234.5); //Decimal number "1234.5" $format->format(1234.5,'c'); //Default currency "$1234.50" $format->format(0.25, 'p') //Percent "25%" Currency is formated using the localized currency pattern. For example to format the number as Japanese Yen: $ja = new NumberFormat('ja_JP'); Japanese currency pattern, and using Japanese Yen symbol $ja->format(123.14,'c','JPY'); //�?123 (Yen 123) For each culture, the symbol for each currency may be different.
コード例 #1
0
ファイル: NumberFormatTest.php プロジェクト: pradosoft/prado
 function testLocalizedCurrencyFormats2()
 {
     $it = new NumberFormat('it_IT');
     $number = 12.41;
     $wanted = '12,41';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10.23;
     $wanted = '10,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10010.23;
     $wanted = '10.010,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $old = setlocale(LC_ALL, "0");
     setlocale(LC_ALL, "it_IT");
     $number = 12.41;
     $wanted = '12,41';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10.23;
     $wanted = '10,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     $number = 10010.23;
     $wanted = '10.010,23';
     $this->assertEquals($wanted, $it->format($number, 'd'));
     setlocale(LC_ALL, $old);
 }
コード例 #2
0
ファイル: TNumberFormat.php プロジェクト: pradosoft/prado
 /**
  * Formats the localized number, be it currency or decimal, or percentage.
  * If the culture is not specified, the default application
  * culture will be used.
  * @return string formatted number
  */
 protected function getFormattedValue()
 {
     $value = $this->getValue();
     $defaultText = $this->getDefaultText();
     if (empty($value) && !empty($defaultText)) {
         return $this->getDefaultText();
     }
     $app = $this->getApplication()->getGlobalization();
     //initialized the default class wide formatter
     if (self::$formatter === null) {
         self::$formatter = new NumberFormat($app->getCulture());
     }
     $pattern = strlen($this->getPattern()) > 0 ? $this->getPattern() : $this->getType();
     $culture = $this->getCulture();
     //return the specific cultural formatted number
     if (!empty($culture) && $app->getCulture() != $culture) {
         $formatter = new NumberFormat($culture);
         return $formatter->format($this->getValue(), $pattern, $this->getCurrency(), $this->getCharset());
     }
     //return the application wide culture formatted number.
     return self::$formatter->format($this->getValue(), $pattern, $this->getCurrency(), $this->getCharset());
 }