コード例 #1
0
        public double Convert(String from, double amount, string to)
        {
            double fromRate = provider.GetExchangeRate(from);
            double toRate   = provider.GetExchangeRate(to);

            return(amount * toRate / fromRate);
        }
コード例 #2
0
        public async Task <decimal> Convert(decimal fromAmount, Currency fromCurrency, Currency toCurrency)
        {
            try
            {
                if (fromAmount <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(fromAmount), "Must be greater than zero.");
                }
                if (fromCurrency is null)
                {
                    throw new ArgumentNullException(nameof(fromCurrency));
                }
                if (toCurrency is null)
                {
                    throw new ArgumentNullException(nameof(toCurrency));
                }
                _logger.LogInformation($"Converting {fromAmount} {fromCurrency.Code} to {toCurrency.Code}.");
                var fromToBaseAmount = fromAmount * await _exchangeRateProvider.GetExchangeRate(fromCurrency);

                return(fromToBaseAmount / await _exchangeRateProvider.GetExchangeRate(toCurrency));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error while converting currency {fromAmount} of \"{fromCurrency}\" to \"{toCurrency}\"");
                throw;
            }
        }