Description:-
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 browser)
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 function called
Convert with three parameter (amount, fromCurrency, toCurrency) which return
Exchange rate in decimal.
Default.aspx:-
<table> <tr> <td align="right">Enter Amount:</td> <td><asp:TextBox ID="txtAmount" runat="server"></asp:TextBox></td> </tr> <tr> <td align="right">From:</td><td> <asp:DropDownList ID="DDLFrom" runat="server"> <asp:ListItem Value="USD">US Dollar (USD)</asp:ListItem> <asp:ListItem Value="AED">United Arab Emirates Dirham (AED)</asp:ListItem> <asp:ListItem Value="CAD">Canadian Dollar (CAD)</asp:ListItem> <asp:ListItem Value="INR">Indian Rupee (INR)</asp:ListItem> <asp:ListItem Value="EUR">Euro (EUR)</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td align="right">to:</td> <td> <asp:DropDownList ID="DDLFrom" runat="server"> <asp:ListItem Value="USD">US Dollar (USD)</asp:ListItem> <asp:ListItem Value="AED">United Arab Emirates Dirham (AED)</asp:ListItem> <asp:ListItem Value="CAD">Canadian Dollar (CAD)</asp:ListItem> <asp:ListItem Value="INR">Indian Rupee (INR)</asp:ListItem> <asp:ListItem Value="EUR">Euro (EUR)</asp:ListItem> </asp:DropDownList> </td> </tr> <asp:Button ID="btnConvert" runat="server" Text="Convert" OnClick="btnConvert_Click" /></td></tr> </table>
protected void btnConvert_Click(object sender, EventArgs e) { string url = string.Format("https://www.igolder.com/exchangerate.ashx?from={0}&to={1}", DDLFrom.SelectedItem.Value.ToUpper(), DDLTo.SelectedItem.Value.ToUpper()); string response = web.DownloadString(url); // Regex regex = new Regex("rhs: \\\\\\"(\\\\d*.\\\\d*)"); Regex regex = new Regex("rhs: \"(\\d*.\\d*)"); Match match = regex.Match(response); Label1.Text = Convert.ToDecimal(Convert.ToDecimal(response) * Convert.ToDecimal(txtAmount.Text)).ToString(); }
Now if you check you will get Current Rate of that Currency from Server.
1 comments:
commentsI enjoyed this and I will be back for more. Foreign Currency Exchange San Francisco
ReplyThanks for comments.....