C#从XE获取当前汇率

我需要在我的应用程序上显示当前汇率。是否可以从http://www.xe.com(XE 转换器)中检索汇率在这里我尝试了什么:


我需要在我的应用程序上显示当前汇率。
是否可以从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 Web().DownloadString(url);
            // Convert string to number
            decimal exchangeRate =
                decimal.P(response, System.Globalization.CultureInfo.InvariantCulture);
            // Output the result
            Output = (amount1 * exchangeRate).ToString();
            textBox2.Text = Output;
            return Output;
        }

有了这个代码,我没有完整的输出...小数部分不是
显示...

3

是的,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 Web())
    {
        var response = wc.DownloadString(url);
        decimal exchangeRate = decimal.P(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
EDIT

使用 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 Web())
        {
            var json = wc.DownloadString(url);
            Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.P(json);
            decimal exchangeRate = (decimal)token.SelectToken("rate");
            return (amount * exchangeRate).ToString();
        }
    }

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(867)
请求 utl_http包时 Oracle错误“ORA-28759:无法打开文件”
上一篇
在给定范围内查找函数的根(root of the function)
下一篇

相关推荐

  • vb如何编写程序代码Private Sub Form_Load() ' 设置窗口标题 Me.Caption = He

    示例示例VB程序代码的编写是通过使用Visual Basic编程语言来实现的。下面是一个简单的VB程序代码示例:'示例代码…

    2023-03-14 15:57:06
    0 98 49
  • code键 A Guide to Success

    示例示例code键是一个特殊的键,它可以用来输入代码。它通常位于键盘的右上角,并且有一个特殊的图标,如“#”或“@”。下面是一个简单的代码示例,使用code键来输入代码:…

    2023-02-13 08:43:06
    0 70 65
  • type c接口是啥改变你的充电体验

    示例示例Type C接口是一种新型的USB连接器,它可以支持更高的数据传输速度,比传统的USB 0接口更快。与传统的USB接口不同,Type C接口可以在两端都使用,无需额外的翻转,使连接更加方便快捷。下面是一个使用Type C接口的代码示例:…

    2023-01-12 02:06:09
    0 28 62
  • good catch什么意思:好抓住!让你的梦想变成现实

    示例示例Good catch是一种表扬语,用来表扬某人发现了一个有价值的信息或问题。它可以用来表扬别人的观察力、思考能力和分析能力。例如:…

    2023-02-17 03:14:17
    0 92 72
  • carwings是什么意思智能汽车连接系统的未来

    Carwings是日产汽车公司推出的一款远程智能汽车服务,它可以帮助用户远程监控汽车的状态和行驶信息,并且可以远程控制汽车的功能。…

    2023-01-19 13:55:38
    0 98 25
  • cvt变速箱油分几种:CVT变速箱油的种类及用法

    cvt变速箱油一般分为三种,分别是:传统cvt变速箱油:代码:CVT-1…

    2023-01-27 12:23:25
    0 78 29
  • cv人生履历:从零开始,一步步走向成功——[姓名]的职业生涯

    cv人生履历是一种概括性的文件,用于描述一个人的教育背景、工作经历、技能、专业知识和其他重要信息的文件。它可以帮助雇主评估求职者的资格,并决定是否面试或录用他们。…

    2023-02-01 13:25:09
    0 61 99
  • cs go 国际服挑战国际赛场,体验全新竞技乐趣

    CS GO国际服是一种多人在线游戏,由Valve Corporation开发,可以在Steam平台上进行游戏。它是一款基于Counter-Strike系列游戏的竞技游戏,玩家可以在两个不同的队伍中进行游戏,一个是攻击者(Terrorists),另一个是防守者(Counter-Terrorists)。玩家可以通过购买武器,装备,技能,以及其他道具来提高游戏能力。…

    2023-03-08 00:34:44
    0 10 82

发表评论

登录 后才能评论

评论列表(61条)