We help IT Professionals succeed at work.

C# Get Current Exchange Rate from XE

SniperCode Sheva
SniperCode Sheva used Ask the Experts™
on
Hi,

I need to display current exchange rates on my application.
Is it possible to retrieve the exchange rate from http://www.xe.com (XE Converter)
Thank you.
Here what I tried:
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
        {
            string Output = "";
             string fromCurrency1 = comboBox1.Text;
             string toCurrency1 = comboBox2.Text;
             decimal amount1 = Convert.ToDecimal(textBox1.Text);

            // For other currency symbols see http://finance.yahoo.com/currency-converter/
            // Construct URL to query the Yahoo! Finance API

            const string urlPattern = "http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1";
            string url = string.Format(urlPattern, fromCurrency1, toCurrency1);

            // Get response as string
            string response = new WebClient().DownloadString(url);

            // Convert string to number
            decimal exchangeRate =
                decimal.Parse(response, System.Globalization.CultureInfo.InvariantCulture);

            // Output the result
            Output = (amount1 * exchangeRate).ToString();
            textBox2.Text = Output;

            return Output;
        }

Open in new window

With this code I am not having the full output... the decimal part is not showing...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
I just tried your code and it is working correctly for me. What is your decimal point separator? A dot?

        private void button1_Click(object sender, EventArgs e)
        {
            CurrencyConversion(new decimal(123.45), "CAD", "USD");
        }

        public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
        {
            string Output = "";
            string fromCurrency1 = fromCurrency; // comboBox1.Text;
            string toCurrency1 = toCurrency; // comboBox2.Text;
            decimal amount1 = amount; // Convert.ToDecimal(textBox1.Text);

            // For other currency symbols see http://finance.yahoo.com/currency-converter/
            // Construct URL to query the Yahoo! Finance API

            const string urlPattern = "http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1";
            string url = string.Format(urlPattern, fromCurrency1, toCurrency1);

            // Get response as string
            string response = new WebClient().DownloadString(url);

            // Convert string to number
            decimal exchangeRate = decimal.Parse(response, System.Globalization.CultureInfo.InvariantCulture);

            // Output the result
            Output = (amount1 * exchangeRate).ToString();
            //textBox2.Text = Output;
            MessageBox.Show(Output);

            return Output;
        }

Open in new window

Author

Commented:
instead of CAD, try GNF  and put as an amount 10000 and try it here http://www.xe.com/en/currencyconverter/convert/?Amount=10000&From=GNF&To=USD it will not be the same output...
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
It is working. The returned exchange rate is 0.0001. so 10,000 * 0.00001 = 1.

Replace 10000 with 123.45 and you will see that it is working.

Author

Commented:
Why I must replace 10000 with 123.45 ? Did you see the result in the link that I gave ?
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
>>Why I must replace 10000 with 123.45 ?

10,000 * 0.00001 = 1.

Try it with 123.45 and you will see decimals

Author

Commented:
yes but the real rate is 0.000108225 and not 0.00001 so I must get this : 0.000108225
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
the service that is called seems to be limited to 5 digits as it returns only .00001. You can see it in the response variable.
Top Expert 2016

Commented:
converted = amount1 * exchangeRate;
            Output = string.Format("{0:f2}", converted);
            textBox2.Text = Output;

Open in new window


you code also worked for me
Asp.Net Developer
Commented:
Hello,

You can use the Google tool.
public double CurrencyConversion(double amount, string fromCurrency, string toCurrency)
    {
        double ret = 0.0;
        double rate = 1;

        try
        {
            WebClient web = new WebClient();

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

            string response = web.DownloadString(url);

            int fromCurLength = response.IndexOf("1 " + fromCurrency);
            string tempResponse = response.Substring(fromCurLength);
            string[] doubleArray = Regex.Split(tempResponse, @"[^0-9\.]+");

            string amountTemp = doubleArray[1];
            amountTemp = Regex.Replace(amountTemp, @"\s", "");
            rate = System.Convert.ToDouble(amountTemp);
        }
        catch (Exception ex)
        {
            rate = 1;
        }

        ret = amount * rate;
        return ret;
    }

Open in new window