2013-09-17 11 views
1


Я использую Intl libary для форматирования чисел с символом валюты.NumberFormatter символ валюты (preend)

$number = new NumberFormatter('es_ES', NumberFormatter::CURRENCY); 
echo $number->format('234234.234324'); 

Моя локальная версия PHP:

version 1.1.0 
ICU version 50.1.2 
ICU Data version 50.1 

Сервер:

version 1.1.0 
ICU version 50.1.2 

Результаты:
Моя версия: 234.234,23 €
Версия сервера: € 234.234,23

символ валюты должен быть в конце номера вместо того, чтобы быть в начале номера

Благодаря

ответ

0

я сделал это в качестве помощника.

/** 
* Number format wrapper 
*/ 
class NumberFormat extends Helper\NumberFormat { 
    private $serviceLocator; 

    public function setServiceLocator($service) { 
     $this->serviceLocator = $service; 
    } 
    /** 
    * Number format 
    * @param type $number 
    * @param type $decimals 
    * @param type $formatStyle 
    * @param string $currency 
    * @param type $locale 
    * @return string 
    */ 
    public function __invoke(
     $number, 
     $decimals = null, 
     $formatStyle = null, 
     $currency = null, 
     $locale  = null 
    ) { 

     $currency = strtoupper($currency); 
     //get current locale 
     $currentLocale = $this->serviceLocator->get('translator')->getLocale(); 
     $config = $this->serviceLocator->get('config'); 

     //mapping between countries 
     if(isset($config['application-options']['currency-symbol-map'][$currentLocale])) { 
      $currenciesMapping = $config['application-options']['currency-symbol-map']; 
      $localeCurrency = $currenciesMapping[$currentLocale]; 
      //England pound != euro 
      if(strtolower($currency) != $localeCurrency) { 
       $locale = array_search($currency,$currenciesMapping); 
      } 
     }; 

     if (!$locale) { 
      $locale = $currentLocale; 
     } 
     if (null === $formatStyle) { 
      $formatStyle = $this->getFormatStyle(); 
     } 

     if (!is_int($decimals) || $decimals < 0) { 
      $decimals = $this->getDecimals(); 
     } 
     // 6.000000 should be 6.00 and decimals more than 2 
     $numberExplode = explode('.',$number); 

     if($decimals > 2 && $numberExplode[1] == 0) { 
      $decimals = 2; 
     } 

     $formatType = $this->getFormatType(); 

     $formatterId = md5($formatStyle . "\0" . $locale . "\0" . $decimals); 

     if (!isset($this->formatters[$formatterId])) { 
      $this->formatters[$formatterId] = new NumberFormatter(
       $locale, 
       $formatStyle 
      ); 

      if ($decimals !== null) { 
       $this->formatters[$formatterId]->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals); 
       $this->formatters[$formatterId]->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals); 
      } 
     } 

     //modify pattern 
     //always at the begining 
     if($formatStyle == \NumberFormatter::CURRENCY) { 
      $pattern = str_replace('¤','',$this->formatters[$formatterId]->getPattern()); 
      $this->formatters[$formatterId]->setPattern('¤'.$pattern); 
     } 
     if($currency == null) 
      return $this->formatters[$formatterId]->format($number, $formatType); 
     else 
      return $this->formatters[$formatterId]->formatCurrency($number,$currency); 
    } 
} 
Смежные вопросы