示例#1
0
        public static decimal ConvertTo(decimal fromAmount, CurrencyType fromCurrencyType, CurrencyType toCurrencyType, Currency[] currencies)
        {
            if (fromCurrencyType == toCurrencyType)
                return fromAmount;

            if ((fromCurrencyType != CurrencyType.CZK && currencies.All(c => c.CurrencyType != fromCurrencyType)) ||
                toCurrencyType != CurrencyType.CZK && currencies.All(c => c.CurrencyType != toCurrencyType))
            {
                throw new Exception(String.Format(ValidationResource.Currency_CannotProccessCurrencyChange_ErrorMessage, fromCurrencyType, toCurrencyType));
            }

            decimal amountTo = fromAmount;
            Currency currency;

            // Převod konta na CZK
            if (fromCurrencyType != CurrencyType.CZK)
            {
                currency = currencies.Single(c => c.CurrencyType == fromCurrencyType);
                amountTo = amountTo * currency.ExchangeRateToCZK;
            }

            // Převod konta na aktuální měnu
            if (toCurrencyType != CurrencyType.CZK)
            {
                currency = currencies.Single(c => c.CurrencyType == toCurrencyType);
                amountTo = amountTo / currency.ExchangeRateToCZK;
            }

            return amountTo;
        }