C#从XE获取当前汇率
C#从XE获取当前汇率,c#,rate,C#,Rate,我需要在我的应用程序上显示当前汇率。 是否可以从(XE转换器)检索汇率 这里是我尝试的: public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency) { string Output = ""; string fromCurrency1 = comboBox1.Text; string to
我需要在我的应用程序上显示当前汇率。
是否可以从(XE转换器)检索汇率
这里是我尝试的:
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
string Output = "";
string fromCurrency1 = comboBox1.Text;
string toCurrency1 = comboBox2.Text;
decimal amount1 = Convert.ToDecimal(textBox1.Text);
// For other currency symbols see http://finance.yahoo.com/currency-converter/
// Construct URL to query the Yahoo! Finance API
const string urlPattern = "http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1";
string url = string.Format(urlPattern, fromCurrency1, toCurrency1);
// Get response as string
string response = new WebClient().DownloadString(url);
// Convert string to number
decimal exchangeRate =
decimal.Parse(response, System.Globalization.CultureInfo.InvariantCulture);
// Output the result
Output = (amount1 * exchangeRate).ToString();
textBox2.Text = Output;
return Output;
}
使用此代码,我没有完整的输出。。。小数部分不显示…是XE提供了一个,但它只支付。
不允许使用自动工具提取数据。()
我试过你的代码,它对我有效。您对的确切含义是什么?小数部分不显示
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
string url = string.Format(urlPattern, fromCurrency, toCurrency);
using (var wc = new WebClient())
{
var response = wc.DownloadString(url);
decimal exchangeRate = decimal.Parse(response, CultureInfo.InvariantCulture);
return (amount * exchangeRate).ToString("N3");
}
}
测试代码:
Console.WriteLine($"$ 5 = € {CurrencyConversion(5m, "USD", "EUR")}");
Console.WriteLine($"£ 20 = $ {CurrencyConversion(20m, "GBP", "USD")}");
结果:
$ 5 = € 4,661
£ 20 = $ 25,616
编辑
使用NuGet获取Newtonsoft.Json
PM> Install-Package Newtonsoft.Json
代码:
是的,XE提供免费服务,但只提供付费服务。
不允许使用自动工具提取数据。()
我试过你的代码,它对我有效。您对的确切含义是什么?小数部分不显示
public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
{
string url = string.Format(urlPattern, fromCurrency, toCurrency);
using (var wc = new WebClient())
{
var response = wc.DownloadString(url);
decimal exchangeRate = decimal.Parse(response, CultureInfo.InvariantCulture);
return (amount * exchangeRate).ToString("N3");
}
}
测试代码:
Console.WriteLine($"$ 5 = € {CurrencyConversion(5m, "USD", "EUR")}");
Console.WriteLine($"£ 20 = $ {CurrencyConversion(20m, "GBP", "USD")}");
结果:
$ 5 = € 4,661
£ 20 = $ 25,616
编辑
使用NuGet获取Newtonsoft.Json
PM> Install-Package Newtonsoft.Json
代码:
尝试GNF而不是GBP,并将其作为金额10000,然后在此处尝试。输出结果将不同……问题是,使用Yahoo时,您只能获得00001
作为费率,而实际费率为0000108225
。试着看一看。所以,我必须使用雅虎以外的其他API?你可以尝试其他一些API,并使用精度最高的API。你知道哪一个是最好的吗?而不是GBP,尝试GNF并将其作为一个数量10000,然后在此处尝试,结果将不同……问题是,使用Yahoo时,您只能获得00001
作为费率,而实际费率为0000108225
。试着看一看。所以,我必须使用雅虎以外的其他API?你可以尝试其他一些API,并使用精度最高的API。你知道哪一个是最好的吗?