C# Currency Converter using Google API

Currency conversion in C# using Google API

  • There are so many ways to convert amount from one currency to another currency like using google finance converter, Yahoo finance and rate-exchange.
  • In this post i will show conversion using Google Finance Converter.
  • In below code, I create one WebRequest for Google Finance Converter which will return result of arguments which are attached with it.
  • Get the response of WebRequest in form of Stream and stored it in streamReader (which is the object of StreamReader Class).
  • By using, Regex fetched converted amount with desired currency code and stored this result into a string variable and again using Regex fetched only decimal part from generated string.
Hope this post will help you to get the exact result that you want.
public string CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
{
      decimal currency = 0;
      string convertedAmount = "0";
      try
      {
          string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
          WebRequest request = WebRequest.Create(url);
          StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream(), System.Text.Encoding.ASCII);
          string result = Regex.Matches(streamReader.ReadToEnd(), "([^<]+)")[0].Groups[1].Value;
          string rs = new Regex(@"^\D*?((-?(\d+(\.\d+)?))|(-?\.\d+)).*").Match(result).Groups[1].Value;
          if (decimal.TryParse((new Regex(@"^\D*?((-?(\d+(\.\d+)?))|(-?\.\d+)).*").Match(result).Groups[1].Value), out currency))
          {
              convertedAmount = currency.ToString("0.00");
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
      return convertedAmount;
}

Comments

  1. On this subject internet page, you'll see my best information, be sure to look over this level of detail. 2850 cad to usd

    ReplyDelete
  2. It's superior, however , check out material at the street address. currency tracker

    ReplyDelete

Post a Comment

Popular posts from this blog

Display image from byte array in ASP.NET MVC

Export excel and Pdf with Kendo UI MVC c#

Convert a color image to black and white image in C#