format() public method

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.
public format ( $number, $pattern = 'd', $currency = 'USD', $charset = 'UTF-8' ) : string
return string formatted number string
Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 /**
  * 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());
 }