exchangeTo method

Money exchangeTo(
  1. Money from,
  2. CurrencyIsoCode to, {
  3. bool useInversion = true,
})

Converts from to the to currency using a regisetered exhange rate.

If no exchange exists but an inverted rate exists then the inverted rate will be used unless useInversion is false.

If no exchange can be calculated an UnknownExchangeRateException will be thrown.

Implementation

Money exchangeTo(Money from, CurrencyIsoCode to, {bool useInversion = true}) {
  var exchangeRate = exchangeMap[_generate(from.currency.isoCode, to)];

  if (exchangeRate != null) {
    return exchangeRate.applyRate(from);
  }

  /// try the inverse isoCode pair
  exchangeRate = exchangeMap[_generate(
    to,
    from.currency.isoCode,
  )];

  if (exchangeRate != null && useInversion) {
    return exchangeRate.applyInverseRate(from);
  }
  throw UnknownExchangeRateException(from.currency.isoCode, to);
}