gpt4 book ai didi

c# - 谷歌货币转换器

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:04 24 4
gpt4 key购买 nike

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

"Input string was not in a correct format."

在下一行:

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;
}

最佳答案

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

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);

关于c# - 谷歌货币转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com