/// <summary> /// Converts <paramref name="amountToConvert">amountToConvert</paramref> to the base currency. /// </summary> /// <param name="amountToConvert">Amount to convert accounted in <paramref name="currency" />.</param> /// <param name="currency">ISO 4217 code for the currency that the <paramref name="amountToConvert" /> /// is accounted in.</param> /// <param name="currencyRate">Currency rate with respect to the <paramref name="baseCurrency" /> /// for the currency that the <paramref name="amountToConvert" /> is accounted in.</param> /// <param name="baseCurrency">Base currency identified by ISO 4217 code.</param> /// <param name="amountSignificantDigits">Round order applied to the result.</param> /// <param name="currencyRateSignificantDigits">Round order applied to <paramref name="currencyRate" />.</param> /// <exception cref="ArgumentException">Parameters currencyRate, amountSignificantDigits /// and currencyRateSignificantDigits cannot be negative.</exception> /// <remarks>amountToConvert value in baseCurrency units</remarks> public static decimal ToBaseCurrency(this decimal amountToConvert, string currency, decimal currencyRate, string baseCurrency, int amountSignificantDigits = DefaultAmountSignificantDigits, int currencyRateSignificantDigits = DefaultCurrencyRateSignificantDigits) { if (currencyRate < 0.0m) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(currencyRate)), nameof(currencyRate)); } if (amountSignificantDigits < 0) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(amountSignificantDigits)), nameof(amountSignificantDigits)); } if (currencyRateSignificantDigits < 0) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(currencyRateSignificantDigits)), nameof(currencyRateSignificantDigits)); } if (currency.IsBaseCurrency(baseCurrency)) { return(amountToConvert.AccountingRound(amountSignificantDigits)); } decimal validatedCurrencyRate = currencyRate.AccountingRound(currencyRateSignificantDigits); decimal validatedAmount = amountToConvert.AccountingRound(amountSignificantDigits); return((validatedAmount * validatedCurrencyRate).AccountingRound(amountSignificantDigits)); }
/// <summary> /// Converts <paramref name="amountToConvert" /> accounted in currency <paramref name="originalCurrency" /> /// with conversion rate <paramref name="originalCurrencyRate" /> with respect to <paramref name="baseCurrency" /> /// to amount accounted in currency <paramref name="targetCurrency" /> with conversion rate /// <paramref name="targetCurrencyRate" /> with respect to <paramref name="baseCurrency" />. /// </summary> /// <param name="amountToConvert">Amount to convert accounted in <paramref name="originalCurrency" />.</param> /// <param name="originalCurrency">ISO 4217 code for the currency that the <paramref name="amountToConvert" /> is accounted in.</param> /// <param name="originalCurrencyRate">Currency rate with respect to the <paramref name="baseCurrency" /> /// for the currency that the <paramref name="amountToConvert" /> is accounted in. (should be zero or positive)</param> /// <param name="targetCurrency">ISO 4217 code for the currency that the returned value is accounted in.</param> /// <param name="targetCurrencyRate">Currency rate with respect to the <paramref name="baseCurrency" /> /// for the currency that the returned value is accounted in.(should be zero or positive)</param> /// <param name="baseCurrency">Base currency identified by ISO 4217 code.</param> /// <param name="amountSignificantDigits">Round order applied to the result.</param> /// <param name="currencyRateSignificantDigits">Round order applied to <paramref name="originalCurrencyRate" /> /// and <paramref name="targetCurrencyRate" />.</param> /// <param name="defaultAmount">Return value in case a conversion is not possible, /// e.g. <paramref name="targetCurrencyRate" /> is zero.</param> /// <returns><paramref name="amountToConvert" /> converted to <paramref name="targetCurrency" />.</returns> /// <returns>amountToConvert value in targetCurrency units</returns> /// <exception cref="ArgumentException">Parameters originalCurrencyRate, targetCurrencyRate, /// amountSignificantDigits and currencyRateSignificantDigits cannot be negative.</exception> public static decimal ToCurrency(this decimal amountToConvert, string originalCurrency, decimal originalCurrencyRate, string targetCurrency, decimal targetCurrencyRate, string baseCurrency, int amountSignificantDigits = DefaultAmountSignificantDigits, int currencyRateSignificantDigits = DefaultCurrencyRateSignificantDigits, decimal defaultAmount = DefaultAmountDecimal) { if (originalCurrencyRate < 0.0m) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(originalCurrencyRate)), nameof(originalCurrencyRate)); } if (targetCurrencyRate < 0.0m) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(targetCurrencyRate)), nameof(targetCurrencyRate)); } if (amountSignificantDigits < 0) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(amountSignificantDigits)), nameof(amountSignificantDigits)); } if (currencyRateSignificantDigits < 0) { throw new ArgumentException(string.Format(Properties.Resources.ParameterCannotBeNegative, nameof(currencyRateSignificantDigits)), nameof(currencyRateSignificantDigits)); } // zero remains zero in any currency if (amountToConvert.AccountingRound(amountSignificantDigits) == 0.0m) { return(0); } // if the currencies are the same, amount is the same if (originalCurrency.IsSameCurrency(targetCurrency, baseCurrency)) { return(amountToConvert.AccountingRound(amountSignificantDigits)); } // base currency rate with respect to itself is always 1 decimal validatedOriginalCurrencyRate = originalCurrencyRate; if (originalCurrency.IsBaseCurrency(baseCurrency)) { validatedOriginalCurrencyRate = 1.0m; } decimal validatedTargetCurrencyRate = targetCurrencyRate; if (targetCurrency.IsBaseCurrency(baseCurrency)) { validatedTargetCurrencyRate = 1.0m; } // apply currencyRateSignificantDigits validatedOriginalCurrencyRate = validatedOriginalCurrencyRate.AccountingRound(currencyRateSignificantDigits); validatedTargetCurrencyRate = validatedTargetCurrencyRate.AccountingRound(currencyRateSignificantDigits); // do calculus if possible if (validatedTargetCurrencyRate.AccountingRound(currencyRateSignificantDigits) > 0.0m) { return(((amountToConvert * validatedOriginalCurrencyRate).AccountingRound(amountSignificantDigits) / validatedTargetCurrencyRate).AccountingRound(amountSignificantDigits)); } return(defaultAmount.AccountingRound(amountSignificantDigits)); }
/// <summary> /// Gets a string that represents the value formated by the ffdata standarts. /// </summary> /// <param name="value">number to format</param> public static string ToFFDataFormat(this decimal value) { return(value.AccountingRound(2).ToString("#.00", CultureInfo.InvariantCulture)); }