Reputation: 426
I have a very simple sounding winform task in my book. Using windows form. Fetch the latest currency rate in the text box.
1 USD = ??? INR
best solution to display converted currency I thought was to use Process method with a query string...
http://www.xe.com/ucc/convert.cgi?Amount=" + costTextBox.Text.ToString() + "&From=USD&To=INR"
But how to get and separate the value into the text box?
Upvotes: 3
Views: 3368
Reputation: 3365
Instead of trying to scrap the value from the response from xe.com i would suggest to either purchase their services from here or use this free webservice
var result = client.ConversionRate(CurrencyConverterService.Currency.USD,
CurrencyConverterService.Currency.INR);
Upvotes: 4
Reputation: 26917
You can do this with Yahoo currency converter:
This method will give you current rate:
decimal getCurrencyRate(string currFrom, string currTo)
{
decimal result;
using (WebClient c = new WebClient())
{
string data = c.DownloadString(string.Format("http://download.finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=sl1d1t1ba&e=.csv", currFrom, currTo));
string rate = data.Split(',')[1];
var style = NumberStyles.Number;
var culture = CultureInfo.CreateSpecificCulture("en-US");
decimal.TryParse(rate, style, culture, out result);
}
return result;
}
And you use this way:
//convert $50 to INR
decimal val = 50.0M;
//get rate
decimal rate = getCurrencyRate("USD", "INR");
//calculate value in INR
decimal inrVal = val * rate;
Upvotes: 1
Reputation: 72646
Take a look at the Google Finance API, see the following function :
public static decimal Convert(decimal amount, string from, string to)
{
WebClient web = new WebClient();
string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={0}{1}=?{2}", amount, from.ToUpper(), to.ToUpper());
string response = web.DownloadString(url);
Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
Match match = regex.Match(response);
return System.Convert.ToDecimal(match.Groups[1].Value);
}
Then you can use the function in this way :
decimal converted = Convert(3.25, "USD", "EUR");
Upvotes: 2