/** * @param Money $money * @param Currency $to * @param int $roundingMode * @return Money */ public function convert(Money $money, Currency $to, $roundingMode = PHP_ROUND_HALF_UP) { $new = new Money($money->getAmount(), $to); $conversionRate = $this->source->getRateBetween($money->getCurrency(), $to); return $new->multiply($conversionRate, $roundingMode); }
/** * Multiplies current Money amount by a factor returns the result as a new * Money * * @param float $factor Factor * * @return MoneyInterface New money instance with amount multiplied by factor */ public function multiply($factor) { $wrappedMoney = $this->wrappedMoney->multiply($factor); return Money::create($wrappedMoney->getAmount(), $this->getCurrency()); }
/** * Converts amount from one currency to another using the currency's identifier * * Warning this method can lose precision! * * @param \SebastianBergmann\Money\Money $amount * @param \Heystack\Core\Identifier\IdentifierInterface $to * @return \SebastianBergmann\Money\Money * @throws \InvalidArgumentException */ public function convert(Money $amount, IdentifierInterface $to) { if (!($toCurrency = $this->getCurrency($to))) { throw new \InvalidArgumentException("Currency not supported"); } /** @var \Heystack\Ecommerce\Currency\Interfaces\CurrencyInterface $fromCurrency */ $fromCurrency = $amount->getCurrency(); return $amount->multiply($toCurrency->getValue() / $fromCurrency->getValue()); }