Currency Exchange Rate in webpage using C# ASP.NET

NOTE: GOOGLE turn off currency API but now you can https://www.google.com/finance/converter?a=1&from=AUD&to=INR

Some time ago I found the following Google API , which provides the currency conversion rates for free. Maybe you’ll find it useful.
In this article, I’ll explain how to get currency rate in ASP.Net using C#.

For example, 1 AUD = ? INR (1 Australian Dollar = ? Indian Rupee )
To get INR from 1 AUD, we need to call google API in following format: ( you can try by paste this url into your web browse)

http://www.google.com/ig/calculator?hl=en&q=1AUD%3D%3FINR
The number 1 before AUD is the amount of dollars or quantity.

Above Url returns a JSON object with the details.:

{lhs: "1 Australian dollar",rhs: "47.8167309 Indian rupees",error: "",icc: true}

As you can see the result is called rhs, but it combines the resulting value with the text “Indian rupees”. Now we will remove the string and store the result as a decimal.

We implement fuction called Convert with three parameter (amount, fromCurrency, toCurrency) which return Exchange rate in decimal.

Code:

public static decimal Convert(decimal amount, string fromCurrency, string toCurrency)
        {
            WebClient web = new WebClient();
 
            string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
 
            string response = web.DownloadString(url);
 
            Regex regex = new Regex("rhs: \\\\\\"(\\\\d*.\\\\d*)");
            Match match = regex.Match(response);
 
            decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);
 
            return rate;
        }

Thanks

17 responses to “Currency Exchange Rate in webpage using C# ASP.NET”

  1. Its realy a very good code.

    could you please convert this code to asp.net 3.0. I was trying to do that but getting error at the line
    Regex regex = new Regex(“rhs: \”(\d*.\d*)”);

    Thanks in advance.
    Regards,

    Koushik.

  2. Hi,

    I use your code in vb.
    When I convert a value up to 2999 then the code return error.

    web.DownloadString(url) return:
    {lhs: “2?99.00 Malaysian ringgits”,rhs: “1?18.60736 Singapore dollars”,error: “”,icc: true}

    Direct from google return:
    {lhs: “2 999.00 Malaysian ringgits”,rhs: “1 218.60736 Singapore dollars”,error: “”,icc: true}

    Public Shared Function Convert(amount As Decimal, fromCurrency As String, toCurrency As String) As Decimal
    Dim web As New WebClient()
    Dim url As String = String.Format(“http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}”, fromCurrency.ToUpper(), toCurrency.ToUpper(), amount)
    Dim response As String = web.DownloadString(url)
    Dim regex As New Regex(“rhs: \””(\d*.\d*)”)
    Dim match As Match = regex.Match(response)
    Dim rate As Decimal = System.Convert.ToDecimal(match.Groups(1).Value)
    Return rate
    End Function

  3. Hi,

    I use this code temporary fixed my problem

    web.Encoding = System.Text.Encoding.ASCII
    Dim response As String = web.DownloadString(url).Replace(“?”, “”)

    Thank you for this nice code Ashish Patel

  4. sir.. i am using this code, It is working only 3 digit output getting… but for 4 digit output , it is displayed the following error ..

    ‘ input string is not correct format’

    Plz give me solution to solve this problem urgently.

    • use statement decimal rate = System.Convert.ToDecimal(Regex.Replace(match.Groups[1].Value, @”[^\w’\.””&:;-]+”, “”));
      instead of decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);

      Regex.Replace(match.Groups[1].Value, @”[^\w’\.””&:;-]+”, “”) remove other than digit and . from string

  5. […] when compared using the equity and futures markets of 50 billion and 30 billion respectively.When you have time or cash, you will discover a lot of methods to earn extra income like from active…additional income.Inside the Forex currency market place, youve got the flexibility of trading from […]

  6. Thanks for the amazing post….!!! its working very fine……
    we have to use to name spaces in .cs page

    using System.Net;
    using System.Text.RegularExpressions;

    thanks againnnnnnnnn…………..

Leave a Reply