/** * {@inheritdoc} */ public function present($value, array $options = []) { if (!is_numeric($value)) { return $value; } $numberFormatter = $this->numberFactory->create($options); if (floor($value) != $value) { $numberFormatter->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, 2); $numberFormatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 4); } return $numberFormatter->format($value); }
/** * {@inheritdoc} */ public function validate($number, array $options = [], $attributeCode) { if (null === $number || '' === $number) { return null; } $options = $this->getOptions($options); if (isset($options['locale']) && !isset($options['decimal_separator'])) { $numberFormatter = $this->numberFactory->create($options); $options['decimal_separator'] = $numberFormatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); } $constraint = new NumberFormat(); $constraint->decimalSeparator = $options['decimal_separator']; $constraint->path = $attributeCode; return $this->validator->validate($number, $constraint); }
/** * {@inheritdoc} * * Presents a structured price to be readable. If locale option is set, the price is formatted according to * the locale. If no locale option is set, the default is the price amount then the currency symbol. */ public function present($price, array $options = []) { return $this->numberFactory->create(array_merge($options, ['type' => \NumberFormatter::CURRENCY]))->formatCurrency($price['data'], $price['currency']); }
/** * @return array */ public function getFormats() { $options = ['locale' => $this->getCurrentLocale()]; $decimalSeparator = $this->numberFactory->create($options)->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); return ['decimal_separator' => $decimalSeparator, 'date_format' => $this->dateFactory->create($options)->getPattern()]; }