/** * @inheritdoc */ public function setTextAttribute($attr, $value) { if ($this->isExtendedSpelloutAvailable($attr, $value)) { $this->attributes[$attr] = $value; return true; } $this->spelloutExtender = null; return parent::setTextAttribute($attr, $value); }
protected function getDefaultNumberFormatter($currencyCode, $locale = null) { if (is_null($locale)) { $locale = \Locale::getDefault(); } $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $numberFormatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currencyCode); $numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->decimals); return $numberFormatter; }
public function getDisplayedValue($value) { if ($value !== null && $value !== '') { $formatter = new \NumberFormatter($this->locale, $this->style); if ($this->precision !== null) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); } if ($this->ruleSet !== null) { $formatter->setTextAttribute(\NumberFormatter::DEFAULT_RULESET, $this->ruleSet); } if ($this->maxFractionDigits !== null) { $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $this->maxFractionDigits); } $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping); if ($this->style === \NumberFormatter::PERCENT && !$this->fractional) { $value /= 100; } if ($this->style === \NumberFormatter::CURRENCY) { if ($this->currencyCode === null) { $this->currencyCode = $formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE); } if (strlen($this->currencyCode) !== 3) { throw new TransformationFailedException('Your locale definition is not complete, you have to define a language and a country. (.e.g en_US, fr_FR)'); } $value = $formatter->formatCurrency($value, $this->currencyCode); } else { $value = $formatter->format($value); } if (intl_is_failure($formatter->getErrorCode())) { throw new TransformationFailedException($formatter->getErrorMessage()); } if (key_exists((string) $value, $this->values)) { $value = $this->values[$value]; } return $value; } return ''; }
public function testLocalizedFormatMoney() { // check locale \Locale::setDefault('fr_FR'); $this->assertEquals('1 234 567,89 €', $this->formatter->localizedFormatMoney($this->inputMoney)); \Locale::setDefault('en_US'); $this->assertEquals('€1,234,567.89', $this->formatter->localizedFormatMoney($this->inputMoney)); $this->assertEquals('1 234 567,89 €', $this->formatter->localizedFormatMoney($this->inputMoney, 'fr')); // check new currency $money = new Money(123456789, new Currency('USD')); $this->assertEquals('1 234 567,89 $US', $this->formatter->localizedFormatMoney($money, 'fr')); $this->assertEquals('$1,234,567.89', $this->formatter->localizedFormatMoney($money, 'en')); // ckeck decimals $formatter = new MoneyFormatter(4); \Locale::setDefault('fr_FR'); $this->assertEquals('12 345,6789 €', $formatter->localizedFormatMoney($this->inputMoney)); // check with custom formatter $numberFormatter = new \NumberFormatter('fr', \NumberFormatter::CURRENCY); $numberFormatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'EUR'); $numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 3); $this->assertEquals('12 345,679 €', $formatter->localizedFormatMoney($this->inputMoney, null, $numberFormatter)); }
/** * 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; }
/** * This method returns an amount as currency, with a symbol and currency code. * * <code> * // Create currency object. * $currencyId = 1; * $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId); * * // Create amount object. * $options = new Joomla\Registry\Registry(); * $options->set("intl", true); * $options->set("locale", "en_GB"); * $options->set("format", "2/,/."); * * $amount = 1500.25; * * $amount = new Crowdfunding\Amount($amount, $options); * $amount->setCurrency($currency); * * // Return $1,500.25 or 1,500.25USD. * echo $amount->formatCurrency(); * </code> * * @return string */ public function formatCurrency() { $intl = (bool) $this->options->get('intl', false); $fractionDigits = abs($this->options->get('fraction_digits', 2)); $format = $this->options->get('format'); $amount = $this->value; // Use number_format. if (!$intl and \JString::strlen($format) > 0) { $value = $this->formatNumber($this->value); if (!$this->currency->getSymbol()) { // Symbol $amount = $value . $this->currency->getCode(); } else { // Code if (0 === $this->currency->getPosition()) { // Symbol at beginning. $amount = $this->currency->getSymbol() . $value; } else { // Symbol at end. $amount = $value . ' ' . $this->currency->getSymbol(); } } } // Use PHP Intl library. if ($intl and extension_loaded('intl')) { // Generate currency string using PHP NumberFormatter ( Internationalization Functions ) $locale = $this->options->get('locale'); // Get current locale code. if (!$locale) { $lang = \JFactory::getLanguage(); $locale = str_replace('-', '_', $lang->getTag()); } $numberFormat = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $numberFormat->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $this->currency->getCode()); $numberFormat->setAttribute(\NumberFormatter::FRACTION_DIGITS, $fractionDigits); $amount = $numberFormat->formatCurrency($this->value, $this->currency->getCode()); } return $amount; }
/** * @param $culture * @param $style * @param array $attributes * @param array $textAttributes * * @return \NumberFormatter */ protected function getFormatter($culture, $style, $attributes = array(), $textAttributes = array()) { $formatter = new \NumberFormatter($culture, $style); foreach (array_merge($this->textAttributes, $textAttributes) as $name => $value) { $constantName = strtoupper($name); if (!defined('NumberFormatter::' . $constantName)) { throw new \InvalidArgumentException("Numberformatter has no text attribute '{$name}'"); } $formatter->setTextAttribute(constant('NumberFormatter::' . $constantName), $value); } foreach (array_merge($this->attributes, $attributes) as $name => $value) { $constantName = strtoupper($name); if (!defined('NumberFormatter::' . $constantName)) { throw new \InvalidArgumentException("Numberformatter has no attribute '{$name}'"); } $formatter->setAttribute(constant('NumberFormatter::' . $constantName), $value); } return $formatter; }
protected function assertInCodomain($locale, $currencyCode, $value) { $this->assertInternalType('string', $value); $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, $currencyCode); $domainValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE); $testValue = $formatter->formatCurrency($domainValue, $currencyCode); $this->assertSame($value, $testValue); }
<?php $amounts = array(array(152.9, 'USD'), array(328, 'ISK'), array(-1, 'USD'), array(500.53, 'EUR')); $fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $fmt->setAttribute(NumberFormatter::PADDING_POSITION, NumberFormatter::PAD_AFTER_PREFIX); $fmt->setAttribute(NumberFormatter::FORMAT_WIDTH, 15); $fmt->setTextAttribute(NumberFormatter::PADDING_CHARACTER, ' '); foreach ($amounts as $amount) { print $fmt->formatCurrency($amount[0], $amount[1]) . "\n"; }