/** * {@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)); }
/** * {@inheritdoc} */ public function parse($money, $forceCurrency = null) { if (!is_string($money)) { throw new ParserException('Formatted raw money should be string, e.g. $1.00'); } $currencyCode = null; $decimal = $this->formatter->parseCurrency($money, $currencyCode); if (null !== $forceCurrency) { $currencyCode = $forceCurrency; } $currency = new Currency($currencyCode); if (false === $decimal) { throw new ParserException('Cannot parse ' . $money . ' to Money. ' . $this->formatter->getErrorMessage()); } $decimal = (string) $decimal; $subunit = $this->currencies->subunitFor($currency); $decimalPosition = strpos($decimal, '.'); if (false !== $decimalPosition) { $decimalLength = strlen($decimal); $fractionDigits = $decimalLength - $decimalPosition - 1; $decimal = str_replace('.', '', $decimal); $decimal = Number::roundMoneyValue($decimal, $subunit, $fractionDigits); if ($fractionDigits > $subunit) { $decimal = substr($decimal, 0, $decimalPosition + $subunit); } elseif ($fractionDigits < $subunit) { $decimal .= str_pad('', $subunit - $fractionDigits, '0'); } } else { $decimal .= str_pad('', $subunit, '0'); } if ('-' === $decimal[0]) { $decimal = '-' . ltrim(substr($decimal, 1), '0'); } else { $decimal = ltrim($decimal, '0'); } return new Money($decimal, $currency); }
/** * Converte un valore da $this->format in numero senza il simbolo della valuta * * @param $number * @param null $currency * @return bool|float|string */ public function convert($number, $currency = NULL) { is_null($currency) ? $currency = 'EUR' : NULL; $nft = new NF($this->locale, NF::CURRENCY); return $nft->parseCurrency($number, $currency); }