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");
}
}
在这种情况下,生成的货币不包含十进制值。如何获得带有小数部分的确切货币?
最佳答案
有趣的是这个。我运行了你的代码,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。
关于regex - c# - 如何在c#中获得2位小数的正确货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8036679/