base_currency = $base_currency; $this->locale = Locale::getDefault(); } /** * Converts the given value in origin currency to the choosen target currency * @param $value float|string The value that should be converted * @param Currency|null $originCurrency The currency the $value is given in. * Set to null, to use global base currency. * @param Currency|null $targetCurrency The target currency, to which $value should be converted. * Set to null, to use global base currency. * @return string|null The value in $targetCurrency given as bcmath string. * Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing) */ public function convertMoneyToCurrency($value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null) : ?string { $value = (string) $value; //Convert value to base currency $val_base = $value; if($originCurrency !== null) { //Without an exchange rate we can not calculate the exchange rate if ((float) $originCurrency->getExchangeRate() === 0) { return null; } $val_base = bcmul($value, $originCurrency->getExchangeRate(), Pricedetail::PRICE_PRECISION); } //Convert value in base currency to target currency $val_target = $val_base; if ($targetCurrency !== null) { //Without an exchange rate we can not calculate the exchange rate if ($targetCurrency->getExchangeRate() === null) { return null; } $val_target = bcmul($val_base, $targetCurrency->getInverseExchangeRate(), Pricedetail::PRICE_PRECISION); } return $val_target; } }