locked
how to generate a list of Double values if input specifed Min and Max RRS feed

  • Question

  • Hello 

    I want know how to generate a list of Double values if input specifed Min and Max 

    Example Min = 2.0

    Max= 10.00

    Double list with 2.0, 3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0

    public static IEnumerable<double> GetDoublevalueswithinthisRange(double min, double max, double step)
           {
               double i;
               for (i = min; i <= max; i += step)
                   yield return i;
     
               if (i != max + step) // added only because you want max to be returned as last item
                   yield return max;
           }

    this will return a value of integers in it not double for example 

    2,3,4,5,6,7,8,9,10

    can i know why its not format with 2.0 3.0,4.0,5.0.... 

    any alternate solution  simple would be great :)  

    thanks

    Ranjith 

     


    Ranjith@Learner @Microsoft products

    Wednesday, July 6, 2016 8:48 PM

Answers

  • Hi Ranjith

    >> "can i know why its not format with 2.0 3.0,4.0,5.0.... "

    Because your double variable will remove 0 at the end automatically. If it is not 0, it will display properly. The following methods to show double value in the specified string format for your reference.

    string str1 = String.Format("{0:N1}",56789); //result: 56,789.0 
    
    string str2 = String.Format("{0:N2}",56789); //result: 56,789.00 
    
    string str3 = String.Format("{0:N3}",56789); //result: 56,789.000 
    
    string str8 = String.Format("{0:F1}",56789); //result: 56789.0 
    
    string str9 = String.Format("{0:F2}",56789); //result: 56789.00 
    

    You can try the following code and you will understand it.

    static void Main(string[] args)
    {
        IEnumerable<double> ie = GetDoublevalueswithinthisRange(1.0, 10.0, 1.0);
        IEnumerable<double> ie2 = GetDoublevalueswithinthisRange(1.56, 10.0, 1.0);
        StringBuilder sb = new StringBuilder();
        foreach (double item in ie)
        {
            sb.Append(item.ToString("0.0") + ", ");
        }
        Console.WriteLine(sb);
        Console.WriteLine("--------------------");
        Console.WriteLine(string.Join(", ", ie));
        Console.WriteLine("--------------------");
        Console.WriteLine(string.Join(", ", ie2));
        Console.ReadLine();
    
    }
    
    public static IEnumerable<double> GetDoublevalueswithinthisRange(double min, double max, double step)
    {
        double i;
        for (i = min; i <= max; i += step)
            yield return Math.Round(i, 1);
    
        if (i != max + step) // added only because you want max to be returned as last item
            yield return Math.Round(max, 1);
    }

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 7, 2016 8:46 AM