Esempio n. 1
0
        public Money?To(CurrencyIsoCode to)
        {
            Money?       converted = null;
            ExchangeRate rate;

            if (_provider.TryGet(_from.CurrencyCode, to, out rate))
            {
                converted = rate.Apply(_from);
            }
            return(converted);
        }
 /// <summary>
 /// Try to get the exchange rate SOURCE->TARGET
 /// </summary>
 /// <param name="exchangeRates">The exchange rates</param>
 /// <param name="sourceCurrency">The currency to convert from</param>
 /// <param name="targetCurrency">The currency to convert to</param>
 /// <returns>Either the exchange rate, or an exception</returns>
 /// <exception cref="KeyNotFoundException"/>
 public static ExchangeRate Get(this IExchangeRateProvider exchangeRates, Currency sourceCurrency, Currency targetCurrency)
 {
     if (exchangeRates == null)
     {
         throw new ArgumentNullException(nameof(exchangeRates));
     }
     return
         (exchangeRates.TryGet(sourceCurrency, targetCurrency) ??
          throw new KeyNotFoundException(Resources.ExchangeRates_CannotFindExchangeRateForCurrencies));
 }
        /// <summary>
        /// Convert the amount into a different currency
        /// </summary>
        /// <param name="exchangeRates">The exchange rates</param>
        /// <param name="amount">The amount to convert</param>
        /// <param name="targetCurrency">The currency to convert to</param>
        /// <returns>The converted currency</returns>
        public static Money?TryConvert(this IExchangeRateProvider exchangeRates, Money amount, Currency targetCurrency)
        {
            if (exchangeRates == null)
            {
                throw new ArgumentNullException(nameof(exchangeRates));
            }
            var rate = exchangeRates.TryGet(amount.Currency, targetCurrency);

            if (rate == null)
            {
                return(null);
            }
            return(amount * rate);
        }
 /// <summary>
 /// Try to get the exchange rate SOURCE->TARGET
 /// </summary>
 /// <param name="sourceCurrency">The currency to convert from</param>
 /// <param name="targetCurrency">The currency to convert to</param>
 /// <returns>Either the exchange rate, or an exception</returns>
 public ExchangeRate TryGet(Currency sourceCurrency, Currency targetCurrency)
 {
     return
         (_rates.TryGet(sourceCurrency, targetCurrency) ??
          _rates.TryGet(targetCurrency, sourceCurrency));
 }
Esempio n. 5
0
        /// <inheritdoc/>
        public ExchangeRate TryGet(Currency source, Currency target)
        {
            var key = Tuple.Create(source, target);

            return(_rates.GetOrAdd(key, key => _provider.TryGet(key.Item1, key.Item2)));
        }