private static decimal GetCurrencyConversionRatio(CurrencyInfo originalCurrency, CurrencyInfo targetCurrency) { rwlock.AcquireReaderLock(Timeout.Infinite); try { string currencyConversionKey = originalCurrency.ISOCode3 + "To" + targetCurrency.ISOCode3 + "ConversionRate"; if (!_currency_ConversionRateDictionary.ContainsKey(currencyConversionKey)) { decimal conversionRate; if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[currencyConversionKey]) && decimal.TryParse(ConfigurationManager.AppSettings[currencyConversionKey], out conversionRate)) { LockCookie lc = rwlock.UpgradeToWriterLock(Timeout.Infinite); try { if (!_currency_ConversionRateDictionary.ContainsKey(currencyConversionKey)) { _currency_ConversionRateDictionary.Add(currencyConversionKey, conversionRate); } return(_currency_ConversionRateDictionary[currencyConversionKey]); } finally { rwlock.DowngradeFromWriterLock(ref lc); } } else { throw new Exception("Conversion between " + originalCurrency.ISOCode3 + " and " + targetCurrency.ISOCode3 + " is not currently available"); } } else { return(_currency_ConversionRateDictionary[currencyConversionKey]); } } finally { rwlock.ReleaseReaderLock(); } }
/// <summary> /// Converts the provided amount to its equivalent in the provided currency /// </summary> /// <param name="originalAmount"></param> /// <param name="targetCurrency"></param> /// <param name="roundValue"></param> /// <returns>MoneyInfo</returns> public static MoneyInfo ConvertAmount(MoneyInfo originalAmount, CurrencyInfo targetCurrency, bool roundValue) { return(ConvertAmount(originalAmount, targetCurrency, DateTime.UtcNow, roundValue, GetCurrencyConversionRatio(originalAmount.Currency, targetCurrency))); }
/// <summary> /// Converts the provided amount to its equivalent in the provided currency, and conversion rate /// </summary> /// <param name="originalAmount"></param> /// <param name="targetCurrency"></param> /// <param name="currencyConversionRatio"></param> /// <returns>MoneyInfo</returns> public static MoneyInfo ConvertAmount(MoneyInfo originalAmount, CurrencyInfo targetCurrency, decimal currencyConversionRatio) { return(ConvertAmount(originalAmount, targetCurrency, DateTime.UtcNow, true, currencyConversionRatio)); }