【问题标题】:C# Get Current Exchange Rate from XEC# 从 XE 获取当前汇率
【发布时间】:2017-04-23 16:02:29
【问题描述】:


我需要在我的应用程序上显示当前汇率。
是否可以从http://www.xe.com 检索汇率(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;
        }

使用此代码,我没有完整的输出...小数部分没有
显示...

【问题讨论】:

    标签: c# rate


    【解决方案1】:

    是的,XE 提供API,但它只需要付费。 不允许制作自动化工具来提取数据。 (source)

    我试过你的代码,它对我有用。 the decimal part is not showing 到底是什么意思?

    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
    

    代码:

    private const string urlPattern = "http://rate-exchange-1.appspot.com/currency?from={0}&to={1}";
        public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
        {
            string url = string.Format(urlPattern, fromCurrency, toCurrency);
    
            using (var wc = new WebClient())
            {
                var json = wc.DownloadString(url);
    
                Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(json);
                decimal exchangeRate = (decimal)token.SelectToken("rate");
    
                return (amount * exchangeRate).ToString();
            }
        }
    

    【讨论】:

    • 而不是英镑,尝试 GNF 并输入 10000 并在此处尝试xe.com/fr/currencyconverter/convert/… 它不会是相同的输出...
    • 问题是使用雅虎你只能得到0,0001作为费率,而实际费率是0,000108225。试试看this answer
    • 那么,我必须使用 Yahoo 以外的其他 API 吗?
    • 您可以尝试其他一些并使用精度最高的那个。
    • 你知道哪个是最好的吗?
    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2012-07-08
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多