/// <summary>
		/// Convert a supplied <see cref="ExchangeRateData"/> to a new base currency.
		/// </summary>
		/// <param name="oldRates">The currency exchange rate to convert.</param>
		/// <param name="newBaseCurrency">The new base currency.</param>
		/// <param name="rounding">Should the converted exchange rates be rounded (saves space when serializing)</param>
		/// <returns>A new <see cref="ExchangeRateData"/> instance using the new base currency.</returns>
		public static ExchangeRateData ConvertBase(this ExchangeRateData oldRates, string newBaseCurrency, bool rounding = true)
		{
			var newRates = new ExchangeRateData
			{
				Base = newBaseCurrency,
				TimeStamp = oldRates.TimeStamp,
				Rates = new Dictionary<string, decimal>(oldRates.Rates.Count)
			};

			decimal x = oldRates.Rates[newBaseCurrency];

			foreach (var i in oldRates.Rates)
			{
				var rate = i.Value / x;
				if (rounding) rate = decimal.Round(rate, OpenExchangeRoundingPrecision);
				newRates.Rates[i.Key] = rate;
			}

			return newRates;
		}
Example #2
0
        /// <summary>
        /// Convert a supplied <see cref="ExchangeRateData"/> to a new base currency.
        /// </summary>
        /// <param name="oldRates">The currency exchange rate to convert.</param>
        /// <param name="newBaseCurrency">The new base currency.</param>
        /// <param name="rounding">Should the converted exchange rates be rounded (saves space when serializing)</param>
        /// <returns>A new <see cref="ExchangeRateData"/> instance using the new base currency.</returns>
        public static ExchangeRateData ConvertBase(this ExchangeRateData oldRates, string newBaseCurrency, bool rounding = true)
        {
            var newRates = new ExchangeRateData
            {
                Base      = newBaseCurrency,
                TimeStamp = oldRates.TimeStamp,
                Rates     = new Dictionary <string, decimal>(oldRates.Rates.Count)
            };

            decimal x = oldRates.Rates[newBaseCurrency];

            foreach (var i in oldRates.Rates)
            {
                var rate = i.Value / x;
                if (rounding)
                {
                    rate = decimal.Round(rate, OpenExchangeRoundingPrecision);
                }
                newRates.Rates[i.Key] = rate;
            }

            return(newRates);
        }
        /// <summary>
        /// Converts the specified ammount <paramref name="value"/> in currency <paramref name="from"/> to the currency specified in <paramref name="to"/>.
        /// </summary>
        /// <param name="data">The currency exchange rates to use.</param>
        /// <param name="value">The ammount of money.</param>
        /// <param name="from">The currency code of the given money value.</param>
        /// <param name="to">The currency to convert the given money value to.</param>
        /// <returns></returns>
        public static decimal Convert(this ExchangeRateData data, decimal value, string from, string to)
        {
            decimal rate;

            if (data.Base != from)
            {
                if (!data.Rates.TryGetValue(from, out rate))
                {
                    throw new ArgumentException("Currency not available.", "from");
                }
                value /= rate;
            }

            if (data.Base != to)
            {
                if (!data.Rates.TryGetValue(to, out rate))
                {
                    throw new ArgumentException("Currency not available.", "to");
                }
                value *= rate;
            }

            return(value);
        }