简体   繁体   English

C#比较双精度值和一系列值并返回两个文本值的最佳方法是什么

[英]C# What is the best way to compare a double value to a range of values and return two text values

I have a project that is in C# but I don't know the best way to use a value (Which can range from 1500 to -1500) to compare with a range of 9 values and return two string values associated with one of the 9 possible range values. 我有一个使用C#编写的项目,但我不知道使用值(范围从1500到-1500)与9个值范围进行比较并返回与9个值之一相关联的两个字符串值的最佳方法可能的范围值。

So far, I have done some research and I believe that a dictionary would be fastest to do this, so I created one as an example to show you the data: 到目前为止,我已经进行了一些研究,并且我相信字典将是最快的方法,因此我创建了一个字典作为示例来向您显示数据:

class MyTICKObject
{
public double MyDouble  { get; set; }
public string MyString1 { get; set; }
public string MyString2 { get; set; }
}

private Dictionary<double, MyTICKObject>    dictTICK = new  Dictionary<double, MyTICKObject>();

int i = dictTICK.Keys.Count();      
    if( i == 0)
    {
        dictTICK.Add(1000,  new MyTICKObject { MyDouble=1000,  MyString1="Extreme Bullish", MyString2="DarkSeaGreen"});
        dictTICK.Add(600,   new MyTICKObject { MyDouble=600,   MyString1="Strong Bullish",  MyString2="Lime"});
        dictTICK.Add(400,   new MyTICKObject { MyDouble=400,   MyString1="Bullish",         MyString2="Green"});
        dictTICK.Add(100,   new MyTICKObject { MyDouble=100,   MyString1="Positive",        MyString2="DarkGreen"});
        dictTICK.Add(0,     new MyTICKObject { MyDouble=0,     MyString1="Neutral",         MyString2="DarkGray"});
        dictTICK.Add(-100,  new MyTICKObject { MyDouble=-100,  MyString1="Negative",        MyString2="DarkRed"});
        dictTICK.Add(-400,  new MyTICKObject { MyDouble=-400,  MyString1="Extreme Bearish", MyString2="FireBrick"});
        dictTICK.Add(-600,  new MyTICKObject { MyDouble=-600,  MyString1="Strong Bearish",  MyString2="Red"});
        dictTICK.Add(-1000, new MyTICKObject { MyDouble=-1000, MyString1="Extreme Bearish", MyString2="IndianRed"});
    }

What I want to do is compare the incoming TICK values (201,-12,456, etc.) with MyDouble, find it's position between two MyDouble values then return MyString2 to a variable called strStmnt, and return MyString2 to a variable called strColor. 我想要做的是将传入的TICK值(201,-12、456等)与MyDouble进行比较,找到它在两个MyDouble值之间的位置,然后将MyString2返回到名为strStmnt的变量,然后将MyString2返回到称为strColor的变量。

Thank you for your help. 谢谢您的帮助。

Instead of using a dictionary, just make a list (or array if you're happy to write it all at once) since you already ordered it and the MyTICK object contains the stop. 无需使用字典,只需创建一个列表(或数组,如果您愿意一次将其全部写入即可),因为您已经对其进行了排序,并且MyTICK对象包含了停止符。 Then you can just iterate through the list from top to bottom with a while loop until you reach the right range stop values. 然后,您可以使用while循环从上至下遍历列表,直至达到正确的范围停止值。 Also be aware that you've defined a set of stops and assigned names to each stop but really you want one less name than stops to be able to assign the in-between values of the input double. 还应注意,您已经定义了一组停止点,并为每个停止点指定了名称,但实际上,您想要的名称比停止点少,以便能够分配输入double的中间值。

I've given an example below which wouldn't work very well because of the stop-values == names problem I mentioned but it should give you the right idea. 我在下面给出了一个示例,由于我提到的stop-values ==名称问题,该示例无法很好地工作,但它应该为您提供正确的想法。

private MyTICKObject[] list = new MyTICKObject[]
            {
                new MyTICKObject { MyDouble = 1000, MyString1 = "Extreme Bullish", MyString2 = "DarkSeaGreen" },
                new MyTICKObject { MyDouble = 600, MyString1 = "Strong Bullish", MyString2 = "Lime" },
                new MyTICKObject { MyDouble = 400, MyString1 = "Bullish", MyString2 = "Green" },
                new MyTICKObject { MyDouble = 100, MyString1 = "Positive", MyString2 = "DarkGreen" },
                new MyTICKObject { MyDouble = 0, MyString1 = "Neutral", MyString2 = "DarkGray" },
                new MyTICKObject { MyDouble = -100, MyString1 = "Negative", MyString2 = "DarkRed" },
                new MyTICKObject { MyDouble = -400, MyString1 = "Extreme Bearish", MyString2 = "FireBrick" },
                new MyTICKObject { MyDouble = -600, MyString1 = "Strong Bearish", MyString2 = "Red" },
                new MyTICKObject { MyDouble = -1000, MyString1 = "Extreme Bearish", MyString2 = "IndianRed" },
            };

public MyTICKObject findAt(double value)
    {
        foreach (MyTICKObject tick in list)
        {
            if (value < tick.MyDouble)
            {
                return tick;
            }
        }
    throw new ArgumentException("Value out of range");
    }

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

相关问题 识别这些“最小”双值并将其转换为C#Double.MinValue的最佳方法是什么? - What is the best way to recognize and convert these “minimum” double values into C# Double.MinValue? 在C#中,将字符串与null进行比较以及“”返回true的最佳方法是什么 - In C#, what is the best way to compare strings with null and “” return true 比较列表中double的值,并根据值c#显示颜色 - Compare values of double in list and have colour depending on value c# 比较 c# 中两个对象的最佳方法是什么 - What is the best way to compare two objects in c# 在C#中比较复杂对象的两个列表的最佳方法是什么 - What is the best way to compare two list of complex object in c# 返回多个枚举值的最佳方法是什么? (Java和C#) - What's the best way to return multiple enum values? (java and C#) 从方法返回两个值的最佳方法是什么? - What is the best way to return two values from a method? 比较两个通用值的最佳方法? - Best way to compare two generic values? 比较文本文件中的两个字符串值 C# - Compare two string values in a text file C# 比较两个文本文件的特定列值 - C# - Compare specific column values of two text files - C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM