Calculates the exchange rate.

@param CurrencyInterface $currencyFrom The currency we are converting from.
@param CurrencyInterface $currencyTo   The currency we are converting to.

@return float

@throws CurrencyNotConvertibleException
182479
public function calculateExchangeRate(
        CurrencyInterface $currencyFrom,
        CurrencyInterface $currencyTo
    ) {
        $currencyFromIso = $currencyFrom->getIso();
        $currencyToIso = $currencyTo->getIso();

        if ($currencyFromIso == $currencyToIso) {
            return 1.0;
        }

        return $this
            ->calculateExchangeRateBetweenIsos(
                $currencyFromIso,
                $currencyToIso
            );
    }
4248
public function setCurrency($currency = 'USD')
    {
        $allowedCurrencies = ['AUD', 'BRL', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'INR', 'JPY', 'MYR', 'MXN', 'NOK', 'NZD', 'PHP', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'TWD', 'THB', 'USD', 'RUB'];

        // Check if provided currency is valid.
        if (!in_array($currency, $allowedCurrencies)) {
            throw new \Exception('Currency is not supported by PayPal.');
        }

        $this->currency = $currency;

        return $this;
    }
299412
public function setCurrency($value)
    {
        if ($value === null || $value === '') {
            $this->_currency = null;
        } elseif (TypeCast::canCastToEnumString($value)) {
            $value = strtoupper($value);
            if (!CurrencyCode::valueExists($value)) {
                throw new \InvalidArgumentException('Invalid currency value: "' . $value . '"');
            }
            $this->_currency = $value;
        } else {
            throw new \InvalidArgumentException('Invalid currency value type: "' . gettype($value) . '"');
        }
    }
836727
public function set(CurrencyInterface $currency)
    {
        $this
            ->session
            ->set(
                $this->sessionFieldName,
                $currency->getIso()
            );

        return $this;
    }
810984
public function printMoney(MoneyInterface $money = null)
    {
        if (!($money instanceof MoneyInterface)) {
            return '';
        }

        if (!($money->getCurrency() instanceof CurrencyInterface)) {
            return $money->getAmount();
        }

        $moneyFormatter = new NumberFormatter(
            $this->locale->getIso(),
            NumberFormatter::CURRENCY
        );

        /**
         * The precision of the integer amount for a given Money
         * (cents, thousandths, 10-thousandths, etc) should be
         * stored in the Currency object. We assume amounts are
         * represented in cents.
         *
         * Loss of precision due to conversion is possible, but only when
         * displaying prices. This operation does not affect amounts
         */

        return $moneyFormatter
            ->formatCurrency(
                $money->getAmount() / 100,
                $money->getCurrency()->getIso()
            );
    }
760763
public function ceil(MoneyInterface $money, $precision = null)
    {
        if ($precision === null) {
            $precision = $this->informationProvider->getDefaultPrecision($money->getCurrency());
        }
        return $this->factory->create(
            $this->math->ceil($money->getAmount(), $precision),
            $money->getCurrency()
        );
    }
1184426
public function forCurrency($currency)
    {
        if (!isset($this->value[$currency])) {
            Yii::warning('Value for currency $currency was not found. Using fake free value. Most probably, tariff is free', __METHOD__);

            return new Value(['value' => 0, 'price' => 0]);
        }

        return $this->value[$currency];
    }
205664
public static function fromInt(int $value, string $currency) : Price
    {
        return new self($value * self::DEFAULT_PRECISION, $currency);
    }
462599
public function findPriceByCurrency($currency): ?PriceInterface
    {
        if ($currency instanceof \Money\Currency) {
            $currency = $currency->getCode();
        } elseif ($currency instanceof CurrencyInterface) {
            $currency = $currency->getIsoCodeAlpha();
        }

        if (!is_string($currency)) {
            throw new \InvalidArgumentException('$currency has to be a string');
        }

        foreach ($this->prices as $price) {
            if ($price->getCurrency()->getIsoCodeAlpha() === $currency) {
                return $price;
            }
        }

        return null;
    }
322325
public function update(CurrencyUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        if (null !== $currency = CurrencyQuery::create()->findPk($event->getCurrencyId())) {
            $currency
                ->setDispatcher($dispatcher)

                ->setLocale($event->getLocale())
                ->setName($event->getCurrencyName())
                ->setSymbol($event->getSymbol())
                ->setFormat($event->getFormat())
                ->setRate($event->getRate())
                ->setCode(strtoupper($event->getCode()))

                ->save();

            $event->setCurrency($currency);
        }
    }
914578
public function numericCodeFor(Currency $currency)
    {
        if (!$this->contains($currency)) {
            throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode());
        }

        return $this->getCurrencies()[$currency->getCode()]['numericCode'];
    }
1071236
public function convert($amount, $from_currency, $to_currency)
    {
        if ($from_currency === $to_currency) {
            return $amount; // Nothing to convert
        }

        $currency = $this->get($from_currency);
        $target_currency = $this->get($to_currency);

        $exponent = $target_currency['decimals'] - $currency['decimals'];
        $amount *= pow(10, $exponent);

        return $amount * ($currency['conversion_rate'] / $target_currency['conversion_rate']);
    }
685858
public function getAmount(MoneyInterface $money = null, $currency = null)
    {
        if (null === $money) {
            return 0.0;
        }

        $reduced = $this->bank->reduce($money, $currency);

        return $reduced->getAmount();
    }
977423
private function retrieveExchangeRateFor(Currency $currency): void
    {
        $response = $this->client->request('GET', self::EXCHANGE_RATE_API_URL . '/latest', [
            'query' => [
                'access_key' => $this->apiKey,
                'base'       => $this->baseCurrency->getCode(),
            ],
        ]);

        $data = json_decode($response->getBody(), true);

        if ($data['success'] !== true) {
            throw FixerIoException::couldNotRetrieveRates($data['error']);
        }

        if (! array_key_exists($currency->getCode(), $data['rates'])) {
            throw FixerIoException::currencyNotFound($currency->getCode());
        }

        $this->exchangeRates[$currency->getCode()] = $data['rates'][$currency->getCode()];
    }
285629
public function getScalePriceForCurrency($currency = 'EUR')
    {
        $scalePrice = null;
        $prices = $this->entity->getPrices();
        if ($prices) {
            foreach ($prices as $price) {
                if ($price->getCurrency()->getCode() == $currency) {
                    $scalePrice[] = $price;
                }
            }
        }

        return $scalePrice;
    }
524170
protected function createInstantRate(CurrencyPairContract $currencyPair, float $rate): ExchangeRate
    {
        return new ExchangeRate($currencyPair, $rate, new \DateTime(), $this->getName());
    }
770335
private function retrievePriceByCurrency(array &$prices, $currencyId)
    {
        /** @var ProductPrice $price */
        foreach ($prices as $index => $price) {
            if ($currencyId === $price->getCurrency()->getId()) {
                unset($prices[$index]);

                return $price;
            }
        }

        return null;
    }