/// <summary> /// Converts this amount to an equivalent amount in the specified currency. /// <para> /// The result will be expressed in terms of the given currency. /// If conversion is needed, the provider will be used to supply the FX rate. /// /// </para> /// </summary> /// <param name="resultCurrency"> the currency of the result </param> /// <param name="rateProvider"> the provider of FX rates </param> /// <returns> the converted instance, in the specified currency </returns> /// <exception cref="RuntimeException"> if no FX rate could be found </exception> public CurrencyAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.Equals(resultCurrency)) { return(this); } double converted = rateProvider.convert(amount, currency, resultCurrency); return(CurrencyAmount.of(resultCurrency, converted)); }
/// <summary> /// Converts this amount to an equivalent amount in the specified currency. /// <para> /// The result will be expressed in terms of the given currency. /// If conversion is needed, the provider will be used to supply the FX rate. /// /// </para> /// </summary> /// <param name="resultCurrency"> the currency of the result </param> /// <param name="rateProvider"> the provider of FX rates </param> /// <returns> the converted instance, in the specified currency </returns> /// <exception cref="RuntimeException"> if no FX rate could be found </exception> public virtual Money convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (currency.Equals(resultCurrency)) { return(this); } double converted = rateProvider.convert(amount.doubleValue(), currency, resultCurrency); return(Money.of(resultCurrency, converted)); }
//------------------------------------------------------------------------- public JumpToDefault convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { IDictionary <StandardId, double> mutable = new Dictionary <StandardId, double>(); foreach (KeyValuePair <StandardId, double> entry in amounts.entrySet()) { double converted = rateProvider.convert(entry.Value, currency, resultCurrency); mutable[entry.Key] = converted; } return(JumpToDefault.of(resultCurrency, mutable)); }
//------------------------------------------------------------------------- /// <summary> /// Converts this amount to an equivalent amount the specified currency. /// <para> /// The result will be expressed in terms of the given currency. /// If conversion is needed, the provider will be used to supply the FX rate. /// /// </para> /// </summary> /// <param name="resultCurrency"> the currency of the result </param> /// <param name="rateProvider"> the provider of FX rates </param> /// <returns> the converted instance, which should be expressed in the specified currency </returns> /// <exception cref="RuntimeException"> if no FX rate could be found </exception> public CurrencyAmount convertedTo(Currency resultCurrency, FxRateProvider rateProvider) { if (amounts.size() == 1) { return(amounts.first().convertedTo(resultCurrency, rateProvider)); } double total = 0d; foreach (CurrencyAmount amount in amounts) { total += rateProvider.convert(amount.Amount, amount.Currency, resultCurrency); } return(CurrencyAmount.of(resultCurrency, total)); }