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
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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

Avatar of SniperCode Sheva
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...
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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.
Avatar of SniperCode Sheva

ASKER

Why I must replace 10000 with 123.45 ? Did you see the result in the link that I gave ?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

>>Why I must replace 10000 with 123.45 ?

10,000 * 0.00001 = 1.

Try it with 123.45 and you will see decimals
Avatar of SniperCode Sheva

ASKER

yes but the real rate is 0.000108225 and not 0.00001 so I must get this : 0.000108225
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

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.
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
Avatar of Vishal Tankariya
Vishal Tankariya
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo