Avatar of SniperCode Sheva
SniperCode Sheva
 asked on

C# Get Current Exchange Rate from XE

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...
C#

Avatar of undefined
Last Comment
Vishal Tankariya

8/22/2022 - Mon
Éric Moreau

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

SniperCode Sheva

ASKER
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 Moreau

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
SniperCode Sheva

ASKER
Why I must replace 10000 with 123.45 ? Did you see the result in the link that I gave ?
Éric Moreau

>>Why I must replace 10000 with 123.45 ?

10,000 * 0.00001 = 1.

Try it with 123.45 and you will see decimals
SniperCode Sheva

ASKER
yes but the real rate is 0.000108225 and not 0.00001 so I must get this : 0.000108225
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Éric Moreau

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, CD

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

Open in new window


you code also worked for me
ASKER CERTIFIED SOLUTION
Vishal Tankariya

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question