C#美元到欧元的转换始终返回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.

这是我当前的代码

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

唯一有效的方法是将其放入开关中并创建具有这样的值的可变数量

                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;

但是我想要的是用户可以像在我的第一个示例中一样复制欧元的数量,但是始终返回0。 我该如何解决?

评论
  • babay
    babay 回复

    I might be wrong but it doesn't seem like you are changing theactionResult, but only the title and description. 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.