【问题标题】:Google Currency Converter谷歌货币转换器
【发布时间】:2015-11-07 22:29:09
【问题描述】:

我在过去几年中使用过这个代码,但是谷歌似乎改变了他们的一些链接。出于某种原因,我收到此错误消息:

“输入字符串的格式不正确。”

在下一行:

decimal rate = System.Convert.ToDecimal(match.Groups[1].Value);

我的代码:

try
{
    WebClient web = new WebClient();
    string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={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;
}
catch
{
    return 0;
}

【问题讨论】:

    标签: c# regex decimal converter currency


    【解决方案1】:

    您可能不喜欢这种方法,但它可以完成工作。

    WebClient web = new WebClient();
    string url = string.Format("https://www.google.com/finance/converter?a={2}&from={0}&to={1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
    
    string response = web.DownloadString(url);
    
    var split  = response.Split((new string[] { "<span class=bld>"}),StringSplitOptions.None);
    var value = split[1].Split(' ')[0];
    decimal rate = decimal.Parse(value,CultureInfo.InvariantCulture);
    

    【讨论】:

    • 输入字符串的格式不正确。 - 同样的错误:/
    • 成功了!谢谢...如果我可以问,我为什么不喜欢这种方法?
    • @MarkFenech 好吧,一些开发人员不太喜欢使用字符串操作技术来提取数据,但是当它用于这样的事情时,我认为它是理想的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多