/** * Formatação de numero decimal * @param float/integer $value * @param integer $precision * @param string $language * @return float */ public static function decimalNumber($value, $precision = 2, $language = 'pt_BR') { $valDecimal = new \NumberFormatter($language, \NumberFormatter::DECIMAL); $valDecimal->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $precision); $valDecimal->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $precision); return $valDecimal->format((double) $value, \NumberFormatter::TYPE_DOUBLE); }
/** * Returns a preconfigured \NumberFormatter instance * * @return \NumberFormatter */ protected function getNumberFormatter() { $formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL); if ($this->getOption('precision') !== null) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision')); } $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->getOption('grouping')); return $formatter; }
public function render(\Erebot\IntlInterface $translator) { $locale = $translator->getLocale(\Erebot\IntlInterface::LC_NUMERIC); $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 100); $result = (string) $formatter->format($this->value); return $result; }
/** * {@inheritdoc} */ public function convertDefaultToLocalizedFromLocale($number, $locale) { if (null === $number || '' === $number) { return $number; } $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); if (is_numeric($number) && floor($number) != $number) { $numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); $numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 4); } return $numberFormatter->format($number); }
function parse_decimals($number) { // ignore empty strings and return if (empty($number)) { return $number; } $config = get_instance()->config; $fmt = new \NumberFormatter($config->item('number_locale'), \NumberFormatter::DECIMAL); $fmt->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $config->item('quantity_decimals')); $fmt->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $config->item('quantity_decimals')); return $fmt->parse($number); }
/** * {@inheritdoc} */ public function format($value, array $options = []) { if (!is_numeric($value)) { throw new InvalidTypeException(sprintf('The number formatter expects a numeric value, got "%s".', is_object($value) ? get_class($value) : gettype($value))); } $scale = isset($options['scale']) ? $options['scale'] : 2; $rounding = isset($options['rounding']) ? $options['rounding'] : \NumberFormatter::ROUND_HALFUP; $grouping = isset($options['grouping']) ? $options['grouping'] : false; $formatter = new \NumberFormatter($this->localeContext->getLocale(), \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $scale); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $rounding); $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $grouping); return str_replace(" ", ' ', $formatter->format($value)); }
/** * Il valore viene convertito in percentuale con relativo simbolo * * - Se non vengono passati i decimali vengono presentati quelli impostati dal locale * - Se sono presenti vengono presentati sono quelli indicati es. 2 decimali 10,123 -> 10,12%, 10,456 -> 10,46% * * @param $number * @param null $decimal * @return bool|string */ public function percent($number, $decimal = null) { is_null($decimal) ? $decimal = $this->getMaxDigits() : null; $number = $number / 100; $nft = new NF($this->locale, NF::PERCENT); $nft->setAttribute(NF::FRACTION_DIGITS, $decimal); return $nft->format($number); }
/** * {@inheritDoc} */ protected function getNumberFormatter() { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); if (null !== $this->precision) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision); } return $formatter; }
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; }
/** * {@inheritdoc} */ public function format($value) { $uiLocale = $this->getUiLocale(); if (null === $uiLocale) { return $value; } $numberFormatter = new \NumberFormatter($uiLocale, \NumberFormatter::DECIMAL); $numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getDecimalCount($value)); return $numberFormatter->format($value); }
/** * {@inheritdoc} */ public function format($value, array $options = array()) { if (null === $value) { return $options['null_value']; } if (!is_numeric($value)) { throw FormatterException::invalidType($this, $value, 'numeric value'); } $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); if (null !== $options['precision']) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $options['rounding_mode']); } $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $options['grouping']); $value = $formatter->format($value); if (intl_is_failure($formatter->getErrorCode())) { throw FormatterException::intlError($this, $formatter->getErrorMessage()); } return $value; }
/** * Format the specified number to a string with the specified number of * fractional digits. * * @param * double d * The number to format. * @param * int digits * The number of fractional digits. * @return string The number formatted as a string. */ public function format($d, $digits) { $this->synchronized(function ($thread) { if (is_infinite($d) || is_nan($d)) { return "0"; } $this->numberFormatter->setAttribute(\NumberFormatter::GROUPING_USED, false); $this->numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $digits); return $this->numberFormatter->format($d); }); }
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) != '')); }
/** * {@inheritdoc} */ public function validate($value, Constraint $constraint) { if (null === $value || '' === $value) { return; } if (!is_scalar($value)) { throw new UnexpectedTypeException($value, 'string'); } $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_DOWN); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0); $formatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 0); $formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 0); $decimalSeparator = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); $position = 0; $formatter->parse($value, PHP_INT_SIZE == 8 ? $formatter::TYPE_INT64 : $formatter::TYPE_INT32, $position); if (intl_is_failure($formatter->getErrorCode()) || strpos($value, $decimalSeparator) !== false || $position < strlen($value)) { /** @var Integer $constraint */ $this->context->addViolation($constraint->message, ['{{ value }}' => $this->formatValue($value)]); } }
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 ''; }
function parse_decimals($number) { // ignore empty strings and return if (empty($number)) { return $number; } $config = get_instance()->config; $fmt = new \NumberFormatter($config->item('number_locale'), \NumberFormatter::DECIMAL); if (empty($config->item('thousands_separator'))) { $fmt->setAttribute(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, ''); } return $fmt->parse($number); }
/** * {@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); }
/** * {@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); }
/** * convert number expressions to value * * @assert ("34000", 'en') == "$34,000" * @assert ("123456.789", 'en') == "$123,457" * @assert ("1234567890", 'en') == "$1,234,567,890" */ public static function format($value, $locale = "en") { switch ($locale) { case 'ja': return self::formatInJapanese($value); break; case 'en': default: if ($locale = 'en') { $locale = 'en-US'; } $numberFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); // 'en' can not omit decimal places even with this, so set 'en-US' $numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 0); return $numberFormatter->formatCurrency($value, 'USD'); } }
/** * Locale-aware number and monetary formatting. * Due to issues with the operating system functions on some platforms intl extension is used if available * * // In English, "1,200.05" * // In Spanish, "1200,05" * // In Portuguese, "1 200,05" * echo Num::format(1200.05, 2); * * // In English, "1,200.05" * // In Spanish, "1.200,05" * // In Portuguese, "1.200.05" * echo Num::format(1200.05, 2, true); * * @param float $number number to format * @param integer $places decimal places * @param boolean $monetary monetary formatting? * @return string */ public static function format($number, $places, $monetary = false) { if (extension_loaded('intl')) { $mode = $monetary ? \NumberFormatter::CURRENCY : \NumberFormatter::DECIMAL; $formatter = new \NumberFormatter(\Locale::getDefault(), $mode); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $places); $result = $formatter->format($number); return $monetary ? str_replace($formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL), '', $result) : $result; } $info = localeconv(); if ($monetary) { $decimal = $info['mon_decimal_point']; $thousands = $info['mon_thousands_sep']; } else { $decimal = $info['decimal_point']; $thousands = $info['thousands_sep']; } return number_format($number, $places, $decimal, $thousands); }
/** * @dataProvider getParseData */ public function testNumberFormatter($input, $output, $expectExtraChars = false, $maxIcuVersion = null) { if (!class_exists('NumberFormatter')) { $this->markTestSkipped('ext/intl not loaded'); return; } $icuVersion = $this->getIcuVersion(); if ($maxIcuVersion && version_compare($icuVersion, $maxIcuVersion, '>')) { $this->markTestSkipped('ICU Version to big for this test. Version is ' . $icuVersion . ' max allowed ' . $maxIcuVersion); return; } $input = trim($input); $yay = 0; $x = new NumberFormatter("en_US", NumberFormatter::DECIMAL); $x->setAttribute(NumberFormatter::LENIENT_PARSE, true); $parsed = $x->parse($input, NumberFormatter::TYPE_DOUBLE, $yay); $this->assertEquals($output, $parsed); $this->assertEquals($expectExtraChars, $yay < strlen($input)); }
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)); }
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); }
/** * Returns a preconfigured \NumberFormatter instance. * * @return \NumberFormatter */ protected function getNumberFormatter() { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); if (null !== $this->scale) { $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale); $formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode); } $formatter->setAttribute(\NumberFormatter::GROUPING_USED, $this->grouping); return $formatter; }
/** * 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; }
/** * Returns a preconfigured \NumberFormatter instance * * @return \NumberFormatter */ protected function getNumberFormatter() { $formatter = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->getOption('precision')); return $formatter; }
/** * Returns a preconfigured \NumberFormatter instance. * * @return \NumberFormatter */ protected function getNumberFormatter() { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale); return $formatter; }
/** * Returns a pre-configured \NumberFormatter instance. * * @return \NumberFormatter */ private function getNumberFormatter() { /** @var \NumberFormatter $formatter */ static $formatter; if (!$formatter || $formatter->getLocale() !== \Locale::getDefault()) { $formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL); $formatter->setAttribute(\NumberFormatter::GROUPING_USED, false); } return $formatter; }
/** * Returns a machine workable decimal according to locale. * @param string $locale e.g. en_US * @param string $decimal * @param int $max_digits * @param int $min_digits * @return decimal $res */ public static function formatDecimalFromLocale($locale, $decimal, $min_digits = 2, $max_digits = 2) { $f = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); if ($min_digits) { $f->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $min_digits); } if ($max_digits) { $f->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $max_digits); // by default some locales got max 2 fraction digits, that is probably not what you want } return $f->parse($decimal); }
/** * 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()))); }