protected function explodeTestData($locale, $currencyCode) { // valid values for all currencies $validDomainValues = [(double) -10, (double) 0, (double) 10]; // invalid values for all currencies $invalidDomainValues = [-1, 0, 1, null, false, true, "", "123", "foo", [], ['foo' => 'bar'], new \stdClass(), NAN, INF, -INF]; $tmpValid = $validDomainValues; $tmpInvalid = $invalidDomainValues; // Build fraction digits test data $formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); $formatter->setTextAttribute($formatter::CURRENCY_CODE, $currencyCode); $fractionDigits = $formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS); $tmpValid[] = (double) (1 + pow(10, -$fractionDigits)); $tmpInvalid[] = (double) (1 + pow(10, -($fractionDigits + 1))); $pi = pi(); $tmpValid[] = round($pi, $fractionDigits); if ($fractionDigits > 2) { $tmpValid[] = round($pi, $fractionDigits - 1); $tmpValid[] = (double) (1 + pow(10, -($fractionDigits - 1))); } $tmpInvalid[] = round($pi, $fractionDigits + 1); $return = []; foreach ($tmpValid as $value) { $return[] = [$value, true]; } foreach ($tmpInvalid as $value) { $return[] = [$value, false]; } return $return; }
/** * {@inheritdoc} */ public function format(Money $money) { $valueBase = (string) $money->getAmount(); $negative = false; if (substr($valueBase, 0, 1) === '-') { $negative = true; $valueBase = substr($valueBase, 1); } $fractionDigits = $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS); $valueLength = strlen($valueBase); if ($valueLength > $fractionDigits) { $subunits = substr($valueBase, 0, $valueLength - $fractionDigits) . '.'; $subunits .= substr($valueBase, $valueLength - $fractionDigits); } else { $subunits = '0.' . str_pad('', $fractionDigits - $valueLength, '0') . $valueBase; } if ($negative === true) { $subunits = '-' . $subunits; } return $this->formatter->formatCurrency($subunits, $money->getCurrency()->getCode()); }
/** * {@inheritdoc} */ public function parse($money, $forceCurrency = null) { $decimal = $this->formatter->parseCurrency($money, $currency); if ($decimal === false) { throw new ParserException('Cannot parse ' . $money . ' to Money. ' . $this->formatter->getErrorMessage()); } $decimal = (string) $decimal; if (strpos($decimal, '.') !== false) { $decimal = str_replace('.', '', $decimal); } else { $decimal .= str_pad('', $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS), '0'); } if (substr($decimal, 0, 1) === '-') { $decimal = '-' . ltrim(substr($decimal, 1), '0'); } else { $decimal = ltrim($decimal, '0'); } if ($forceCurrency === null) { $forceCurrency = $currency; } return new Money($decimal, new Currency($forceCurrency)); }
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) != '')); }
public function getAttributeDataProvider() { $intlFormatter = new IntlNumberFormatter('en_US', \NumberFormatter::DECIMAL); $maxIntegerDigits = $intlFormatter->getAttribute(\NumberFormatter::MAX_INTEGER_DIGITS); return array(array('parse_int_only', 'DECIMAL', 'en_US', 0), array('parse_int_only', null, 'en_US', 0), array('GROUPING_USED', 'decimal', 'en_US', 1), array(\NumberFormatter::DECIMAL_ALWAYS_SHOWN, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::MAX_INTEGER_DIGITS, \NumberFormatter::DECIMAL, 'en_US', $maxIntegerDigits), array(\NumberFormatter::MIN_INTEGER_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 1), array(\NumberFormatter::INTEGER_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 1), array(\NumberFormatter::MAX_FRACTION_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 3), array(\NumberFormatter::MIN_FRACTION_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::MAX_FRACTION_DIGITS, \NumberFormatter::CURRENCY, 'en_US', 2), array(\NumberFormatter::MIN_FRACTION_DIGITS, \NumberFormatter::CURRENCY, 'en_US', 2), array(\NumberFormatter::FRACTION_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::MULTIPLIER, \NumberFormatter::DECIMAL, 'en_US', 1), array(\NumberFormatter::GROUPING_SIZE, \NumberFormatter::DECIMAL, 'en_US', 3), array(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::DECIMAL, 'en_US', 4), array(\NumberFormatter::ROUNDING_INCREMENT, \NumberFormatter::DECIMAL, 'en_US', 0.0), array(\NumberFormatter::FORMAT_WIDTH, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::PADDING_POSITION, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::SECONDARY_GROUPING_SIZE, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::SIGNIFICANT_DIGITS_USED, \NumberFormatter::DECIMAL, 'en_US', 0), array(\NumberFormatter::MIN_SIGNIFICANT_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 1), array(\NumberFormatter::MAX_SIGNIFICANT_DIGITS, \NumberFormatter::DECIMAL, 'en_US', 6)); }
/** * Numero di decimali impostati per il locale * * @return bool|int */ public function getMaxDigits() { $nft = new NF($this->locale, NF::DECIMAL); return $nft->getAttribute(NF::MAX_FRACTION_DIGITS); }