Sunday, August 13, 2017

Currency Converter using C#

In many project there is need to convert one currency to other currency specially in transaction application. There are ready made third party tools or Web-API by which we can convert currency, but they are paid. In this particular post i will provide a C# code through which one perform currency conversion. For this i have used yahoo finance currency conversion API which gives you real time currency exchange value.

Here we go.

you just need to pass three parameters to following method and you will get output as converted rate from fromCurrency To toCurrency.

    public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
    {
        string Output = "";
        const string fromCurrency1 = "USD";
        const string toCurrency1 = "INR";
        const double amount1 = 2000;

        // 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
        double exchangeRate =
            double.Parse(response, System.Globalization.CultureInfo.InvariantCulture);

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

        return Output;
    }

Thank you.

1 comment:

  1. Play Now On Gambling City - JamBase
    Play Now On Gambling City. Enjoy 김해 출장마사지 exclusive sign up offers 원주 출장마사지 and start 영천 출장마사지 earning FREECREDIT for new 의왕 출장안마 players at JamBase. We have top-notch casino 화성 출장마사지

    ReplyDelete