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
Great posting, thanks. I had to tweak the url string slightly to http://www.google.com/ig/calculator?q={2}{0}%3D%3F{1} but have it working great and very quick.
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.
hi
some how it showing wrong here but right statement is
Regex regex = new Regex(“rhs: \\\”(\\d*.\\d*)”);
thanks
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
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
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
Try a cleaner interface… no html parsing!
https://www.igolder.com/exchangerate.ashx?from=EUR&to=CAD
Change the currency code in the querystring… simple and very fast to use!
System.Net.WebClient client = new System.Net.WebClient();
double rate = double.Parse(client.DownloadString(@”https://www.igolder.com/exchangerate.ashx?from=USD&to=CAD”));
[…] 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 […]
This is great guide, very helpful to my problem, Thanks.
I made un update of Google Currency conversion api callback on C#.net
http://umairaslam.blogspot.com/2012/01/google-currency-converter-code.html
hi
I can not use vb function in my vb code file.
Please say me some suggestion for it.
Thanks
without code i cant tell you why that happen?
How can i Call C# Function in Page Load Event for Conversion…Please port it…
Thanks..
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…………..
Good respond in return of this question with firm arguments and telling the whole
thing regarding that.
Getting “The remote server returned an error: (404) Not Found.” exception.Please help me out