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;
      }

      4 thoughts on “C# Currency Converter using Google API

      • August 6, 2016 at 2:16 AM
        Permalink

        I have a printer hooked up to the computer inside my room. My daughter has a computer in her room and no printer. How can the lady do her homework on her behalf computer, but have it print out on the inkjet printer in my room? Do I require the internet on a single computer or both? Probably not at all?.

        Reply
      • December 9, 2016 at 4:32 PM
        Permalink

        I have witnessed that good real estate agents everywhere are getting set to FSBO Promotion. They are knowing that it’s in addition to placing a poster in the front yard. It’s really regarding building human relationships with these dealers who at some point will become buyers. So, whenever you give your time and energy to supporting these sellers go it alone – the “Law regarding Reciprocity” kicks in. Interesting blog post.

        Reply
      • April 9, 2017 at 12:32 PM
        Permalink

        Hi There! We are looking for some people that are interested in from working their home on a full-time basis. If you want to earn $100 a day, and you don’t mind developing some short opinions up, this is the perfect opportunity for you! Simply check out the link here NOW!

        Reply

      Leave a Reply

      Your email address will not be published. Required fields are marked *