We help IT Professionals succeed at work.

C# Get Current Exchange Rate from XE

SniperCode Sheva
on
3,093 Views
Last Modified: 2017-04-29
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

Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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
CERTIFIED EXPERT
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.
David Johnson, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

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:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION