The following methods allow to convert currencies from one to another.
👀 See here for full details about rate exchangers.
For example:
USD -> RUB = 75.79 / 1
RUB -> USD = 1 / 75.79
Parameters:
Currency|string $currency
- currency you want to convert to.[?Carbon $date = null]
(optional) - historical mode. Pass the date you want to get rate of.
Returns: Money
- new money instance.
Parameters:
Currency|string $currency
- currency you want to convert to.string $rate
- a rate by which monetary object will be multiplied.
Returns: Money
- new money instance.
CurrenciesNotSupportedByRateExchangerException
- is thrown when$currency
argument is not supported by an API rate exchanger.RateExchangerException
- is thrown when$date
argument is in the future. This also may be thrown because of API rate exchanger problems.
In order to convert currencies with real-time rate.
$rub = money_parse('1000', 'RUB');
$usd = $rub->convertTo('USD');
$backRub = $usd->convertTo(currency('RUB'));
// may be true or false depending on API
// or delay between requests or loosing precision
$rub->equals($backRub);
To convert currencies according to the exact date.
use Carbon\Carbon;
$rub = money_parse('1000', 'RUB');
$usd = $rub->convertTo('USD');
$historicalUsd = $rub->convertTo('USD', Carbon::createFromDate(2010, 4, 27));
$usd->equals($historicalUsd); // false
For converting offline, you need to know the rate.
$rate = 75.32;
$rub = money_parse('1000', 'RUB');
$usd = $rub->offlineConvertTo('USD', 1 / $rate);
$backRub = $usd->offlineConvertTo('RUB', $rate / 1);
$rub->isSameCurrency($usd); // false
$rub->isSameCurrency($backRub); // true
$rub->equals($backRub); // may be true or false because of loosing precision
📌 Back to the contents.