public PriceTable ConvertToUSDollar(decimal Amount,string Currency)
{
try
{
var toCurrency = "USD";
var fromCurrency = "AUD";
string url = string.Format("http://www.google.com/ig/calculator?hl=en&q= {2}{0}%3D%3F{1}", fromCurrency .ToUpper(), toCurrency.ToUpper(), 1000);
WebClient web = new WebClient();
string response = web.DownloadString(url);
Regex regex = new Regex("rhs: \\\"(\\d*.\\d*)");
Match match = regex.Match(response);
string rate = (match.Groups[1].Value.Trim());
rate = Regex.Replace(rate, @"\s", "");
decimal Value = Convert.ToDecimal(rate);
var pricetable = new PriceTable()
{
Price = Value
};
return pricetable;
}
catch(Exception e) {
throw new Exception("Error Occoured While Converting");
}
}
在这种情况下,结果货币不包含小数值。如何获得带小数部分的确切货币?
答案 0 :(得分:3)
有趣的一个。我运行了你的代码,并返回了API:
{lhs: "1000 Australian dollars",rhs: "1 028.9 U.S. dollars",error: "",icc: true}
rhs结果的1和0之间有一个空格(或者可能是Unicode逗号字符)。看着你的正则表达式,。实际上是匹配这个角色,如。表示正则表达式中的“任何字符”。匹配实际小数点需要反斜杠。我附加了这个,小数点后面的数字是另一个\ d。我使用@语法使转义更容易阅读,这给出了:
Regex regex = new Regex(@"rhs: \""(\d*.\d*\.\d)");
这导致1028.9被退回。