简体   繁体   English

c#美元到欧元的转换总是返回0

[英]c# dollar to euro conversion is always returning 0

I am trying to make a dollar to euro converter.我正在尝试将美元兑换成欧元。 but strangely enough, it always returns 0. The intention is that the user, for example, 10 dollars to euro and then the result comes out how much 10 dollars than currently is in euros this is my code what I have at the moment.但奇怪的是,它总是返回 0。目的是用户,例如, 10 dollars to euro ,然后结果出来的结果是 10 美元比目前的欧元多多少 这是我目前拥有的代码。

This is my current code这是我当前的代码

            public void EUR_TO_usd(string clipboardText)
            {
                ActionResult actionResult = new ActionResult(clipboardText);
                if (clipboardText.EndsWith(" dollar to euro"))
                {
                    var parts = clipboardText.Split(' ');
                    if (parts.Length == 4)
                    {
                        if (double.TryParse(parts[0], out double amount))
                        {
                            if (parts[1] == "dollar" && parts[2] == "to" && parts[3] == "euro")
                            {
                                string url = "http://api.openrates.io/latest?base=USD";
                                string json = new WebClient().DownloadString(url);
                                var currency = JsonConvert.DeserializeObject<dynamic>(json);
                                double curAmount = amount * Convert.ToSingle(currency.rates.EUR);
                                actionResult.Title = clipboardText;
                                actionResult.Description = string.Format("{0:N2} {1} = {2:N2} {3}", amount, currency["base"], curAmount, "EUR");
                            }
                        }
                    }
                }

the only thing that worked was putting this in a switch and creating a variable amount with a value like this唯一有效的方法是把它放在一个开关中并用这样的值创建一个可变数量

                case "dollar to euro":
                    {
                        string url = "http://api.openrates.io/latest?base=USD";
                        string json = new WebClient().DownloadString(url);
                        var amount = 1;
                        var currency = JsonConvert.DeserializeObject<dynamic>(json);
                        double curAmount = amount * (double)currency.rates.EUR;
                        {
                            actionResult.Title = clipboardText;
                            actionResult.Description = $"{amount:N2} {currency.@base} = {curAmount:N2} EUR";
                        }
                    }
                    return actionResult;

but what I want is that the user can copy the number of euros as in my first example, but that always returns 0. How do I solve this?但我想要的是用户可以像我的第一个例子一样复制欧元的数量,但这总是返回 0。我该如何解决这个问题?

I might be wrong but it doesn't seem like you are changing the actionResult, but only the title and description.我可能错了,但您似乎并未更改 actionResult,而只是更改了标题和描述。 And if you would want to just return a int i would use public int EUR_TO_usd() And i would look into the naming convention in C#, since a function name shoud not look like that imo.如果你只想返回一个 int 我会使用public int EUR_TO_usd()而且我会研究 C# 中的命名约定,因为函数名称不应该看起来像那个 imo。

And of course you need to call your function with that string.当然,您需要使用该字符串调用您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM