Friday, January 31, 2014

C# Currency Converter using Google API

//A very simple currency converter using google API.
private decimal GoogleCurrencyConverter(string fromCurrency, string toCurrency)
    {
        WebClient web = new WebClient();

        string url = string.Format("https://www.google.com/finance/converter?a=1&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper());

        string response = web.DownloadString(url);

        Regex regex = new Regex("(\\d*.\\d*)");
        var r = regex.Match(response);

        return Convert.ToDecimal(r.Groups[1].Value);
    }