Beispiel #1
0
 public function format($amount)
 {
     $realFormat = str_replace("\$", "¤", $this->format);
     $numFormatter = new \NumberFormatter(null, \NumberFormatter::PATTERN_DECIMAL, $realFormat);
     $numFormatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $this->symbol);
     $numFormatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $this->decimalSeparator);
     $numFormatter->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $this->decimalSeparator);
     $numFormatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $this->thousandsSeparator);
     $numFormatter->setSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $this->thousandsSeparator);
     // PHP fix: number of decimal digits is forced without pattern analysis
     // when in currency mode (with currency symbol in format)
     if (strpos($realFormat, "¤") !== FALSE) {
         // Count number of 0 after decimal separator
         $parts = explode(".", $realFormat);
         $decimals = 0;
         if (count($parts) == 2) {
             $decimalPart = $parts[1];
             $decimals = count(explode("0", $decimalPart)) - 1;
         }
         $numFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $decimals);
     }
     return $numFormatter->format($amount);
 }
function to_decimals($number, $decimals, $type = \NumberFormatter::DECIMAL)
{
    // ignore empty strings and return
    // NOTE: do not change it to empty otherwise tables will show a 0 with no decimal nor currency symbol
    if (!isset($number)) {
        return $number;
    }
    $config = get_instance()->config;
    $fmt = new \NumberFormatter($config->item('number_locale'), $type);
    $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $config->item($decimals));
    $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $config->item($decimals));
    if (empty($config->item('thousands_separator'))) {
        $fmt->setAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '');
    }
    $fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $config->item('currency_symbol'));
    return $fmt->format($number);
}
 /**
  * {@inheritdoc}
  */
 public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT)
 {
     $currency_locale = $this->localeDelegator->resolveCurrencyLocale();
     $decimal_position = strpos($amount, '.');
     $number_of_decimals = $decimal_position !== FALSE ? strlen(substr($amount, $decimal_position + 1)) : 0;
     $formatter = new \NumberFormatter($currency_locale->getLocale(), \NumberFormatter::PATTERN_DECIMAL, $currency_locale->getPattern());
     $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $number_of_decimals);
     $formatter->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator());
     $formatter->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $currency_locale->getDecimalSeparator());
     $formatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator());
     $formatter->setSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $currency_locale->getGroupingSeparator());
     $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency->getSign());
     $formatter->setSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL, $currency->getCurrencyCode());
     return $formatted = $formatter->format($amount);
 }
 /**
  * {@inheritdoc}
  */
 public function localize($number, array $options = [])
 {
     if (null === $number || '' === $number) {
         return $number;
     }
     $options = $this->getOptions($options);
     if (isset($options['locale'])) {
         $numberFormatter = new \NumberFormatter($options['locale'], \NumberFormatter::DECIMAL);
         if (isset($options['disable_grouping_separator']) && true === $options['disable_grouping_separator']) {
             $numberFormatter->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '');
         }
         if (floor($number) != $number) {
             $numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2);
             $numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 4);
         }
         return $numberFormatter->format($number);
     }
     $matchesNumber = $this->getMatchesNumber($number);
     if (!isset($matchesNumber['decimal'])) {
         return $number;
     }
     return str_replace(static::DEFAULT_DECIMAL_SEPARATOR, $options['decimal_separator'], $number);
 }
 /**
  * Creates instance of NumberFormatter class of intl extension
  *
  * @param string $locale
  * @param int $style
  * @param array $attributes
  * @param array $textAttributes
  * @param array $symbols
  * @throws \InvalidArgumentException
  * @return IntlNumberFormatter
  */
 protected function getFormatter($locale, $style, array $attributes = array(), array $textAttributes = array(), array $symbols = array())
 {
     $formatter = new IntlNumberFormatter($locale ?: $this->localeSettings->getLocale(), $this->parseStyle($style));
     foreach ($this->parseAttributes($attributes) as $attribute => $value) {
         $formatter->setAttribute($attribute, $value);
     }
     foreach ($this->parseAttributes($textAttributes) as $attribute => $value) {
         $formatter->setTextAttribute($attribute, $value);
     }
     foreach ($this->parseAttributes($symbols) as $symbol => $value) {
         $formatter->setSymbol($symbol, $value);
     }
     return $formatter;
 }
Beispiel #6
0
 /**
  * Return currency amount formatted for display
  *
  * @return StringType
  */
 public function display()
 {
     $formatter = new \NumberFormatter($this->locale->get(), \NumberFormatter::CURRENCY);
     $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $this->symbol->get());
     $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision->get());
     return new StringType(sprintf($this->displayFormat, $formatter->format($this->getAsFloat())));
 }
 protected function getIntlFormatterWithCurrencyStyle()
 {
     if (!$this->isIntlExtensionLoaded()) {
         return null;
     }
     $formatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY);
     $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, 'SFD');
     return $formatter;
 }
Beispiel #8
0
 /**
  * @dataProvider providerFormatWith
  *
  * @param string $money    The string representation of the money to test.
  * @param string $locale   The target locale.
  * @param string $symbol   A decimal symbol to apply to the NumberFormatter.
  * @param string $expected The expected output.
  */
 public function testFormatWith($money, $locale, $symbol, $expected)
 {
     $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
     $formatter->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $symbol);
     $this->assertSame($expected, Money::parse($money)->formatWith($formatter));
 }
Beispiel #9
0
 public function check_number_locale()
 {
     $number_locale = $this->input->post('number_locale');
     $fmt = new \NumberFormatter($number_locale, \NumberFormatter::CURRENCY);
     $currency_symbol = empty($this->input->post('currency_symbol')) ? $fmt->getSymbol(\NumberFormatter::CURRENCY_SYMBOL) : $this->input->post('currency_symbol');
     if ($this->input->post('thousands_separator') == "false") {
         $fmt->setAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, '');
     }
     $fmt->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency_symbol);
     $number_local_example = $fmt->format(1234567890.123);
     echo json_encode(array('success' => $number_local_example != FALSE, 'number_locale_example' => $number_local_example, 'currency_symbol' => $currency_symbol, 'thousands_separator' => $fmt->getAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL) != ''));
 }