Simple library to retrieve exchange rates from BCE.
$ dotnet add package ExchangeRateConverterBasic library to retrieve currencies exchange rates base on European Central Bank.
This library store rates data when the are fetched from the website to reduce call to the website and improve the performance after the first download it's done. If data is older than 1 day then the data will be refresh.
All the tranformations that doesn't have EUR as origin or target are computed as follow:
If there is no rate in a specific date the previous closest one will be returned.
There are some static fields that can be modified to determine where to save rates file data and how many days should check before an error is raised when a determined date does not have rate data.
Static class with main methods to retrieve and use exchange rates.
Return a dictionary with all available rates for specific currencies
Dictionary<DateTime, double>: dictionary with date as Key and Rate as value
Dictionary<DateTime, double> jpyToIdr = ExchangeRateTool.GetCurrencyRates(CurrencyType.JPY, CurrencyType.IDR);
Console.WriteLine(jpyToIdr[new DateTime(2024, 1, 12)]);
Returns a rate from currencyFrom currency type to currencyTo type for specific date or the closest previous value.
double: specific exchange rate for a given date or nearest one.
double rate = ExchangeRateTool.GetExchangeRateAtDate(CurrencyType.USD, CurrencyType.GBP, new DateTime(2024, 1, 12));
Console.WriteLine(rate);
Return a dictionary with all available rates for specific currencies
double: value of the converted amount.
double newAmount = ExchangeRateTool.ConvertAmount(100, CurrencyType.EUR, CurrencyType.USD, new DateTime(2023, 06, 06));
Console.WriteLine(newAmount);
Return the closets rate to the specified rate from the provieded rates
double: specific exchange rate for a given date or nearest one.
double rate = ExchangeRateTool.GetClosestRateToDate(ExchangeRateTool.GetCurrencyRates(CurrencyType.USD, CurrencyType.GBP), new DateTime(2024, 1, 1));
Console.WriteLine(rate);
Class that keeps data about exchange rates between currencies.
Return the closets rate for the current ExchangeRate object given a specific date.
double: specific exchange rate for a given date or nearest one.
ExchangeRate eurToEur = new ExchangeRate(CurrencyType.EUR, CurrencyType.EUR);
Console.WriteLine(eurToEur.Rates.Count);
Console.WriteLine(eurToEur.GetExchangeRate(DateTime.Now));
ExchangeRate eurToUsd = new ExchangeRate(CurrencyType.EUR, CurrencyType.USD);
Console.WriteLine(eurToUsd.Rates.Count);
Console.WriteLine(eurToUsd.GetExchangeRate(new DateTime(2022, 8, 31)));