/** * Retrieve the currency code. * * @return string|null */ public function getCurrencyCode() { if (!isset($this->options['currency_code'])) { if ($this->formatter) { return $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE); } return null; } return $this->options['currency_code']; }
private function getRules($locale, $type) { $formatter = new \NumberFormatter($locale, $type); $rules = []; $rawRules = explode(';', trim($formatter->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS), ';')); $default = $formatter->getTextAttribute(\NumberFormatter::DEFAULT_RULESET); foreach ($rawRules as $rule) { $rules[$rule] = $rule === $default; } return $rules; }
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 ''; }
/** * @inheritdoc */ public function getTextAttribute($attr) { return isset($this->attributes[$attr]) ? $this->attributes[$attr] : parent::getTextAttribute($attr); }
public function testGetTextAttribute() { $this->skipIfIntlExtensionIsNotLoaded(); $intlDecimalFormatter = new \NumberFormatter('en', \NumberFormatter::DECIMAL); $intlCurrencyFormatter = new \NumberFormatter('en', \NumberFormatter::CURRENCY); $stubDecimalFormatter = $this->getStubFormatterWithDecimalStyle(); $stubCurrencyFormatter = $this->getStubFormatterWithCurrencyStyle(); for ($i = 0; $i <= 5; $i++) { $this->assertSame($stubDecimalFormatter->getTextAttribute($i), $intlDecimalFormatter->getTextAttribute($i), $i); $this->assertSame($stubCurrencyFormatter->getTextAttribute($i), $intlCurrencyFormatter->getTextAttribute($i), 'fooo ' . $i); } }
public function getCurrCode($locale = NULL) { if ($locale === NULL) { $locale = $this->locale; } if (extension_loaded('intl')) { $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); return $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE); } }
/** * Mostra o Valor no real Formatado * @param float $number * @param boolean $fixed * @param boolean $symbol * @param integer $decimals * @return string */ public static function nReal($number, $decimals = 2, $symbol = true, $fixed = true) { if (is_null($number) || empty(self::onlyNumbers($number))) { return ''; } $formater = new \NumberFormatter("pt-BR", \NumberFormatter::CURRENCY); $formater->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $fixed ? $decimals : 1); if ($decimals === false) { $decimals = 2; preg_match_all('/[0-9][^0-9]([0-9]+)/', $number, $matches); if (!empty($matches[1])) { $decimals = strlen(rtrim($matches[1][0], 0)); } } $formater->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $decimals); if (!$symbol) { $pattern = preg_replace("/[¤]/", '', $formater->getPattern()); $formater->setPattern($pattern); } else { // ESPAÇO DEPOIS DO SIMBOLO $pattern = str_replace("¤", "¤ ", $formater->getPattern()); $formater->setPattern($pattern); } return $formater->formatCurrency($number, $formater->getTextAttribute(\NumberFormatter::CURRENCY_CODE)); }