Search code examples
c#countdoublesigndigits

Sign and 0 in front of double


I need some mechanism to count numbers from

-178.125 through -1.875 and +000.000 to +180.000 with step 1.875

so it always has plus or minus sign in the front of the number and has three digits, then dot and then three digits again.

The only thing that comes to my mind is a lot of if conditions.

The best for me is solution in C#.

Thank you

Edit:

I need some while cycle like

N = -178.125;
while(n != +180.000)
{
...
N += 1.875
}

And i want the N to go like -178.125, ..., -001.875, +000.000, +001.875,... +180.000

Is this understandable? :D I know I have troubles with specifying things :)


Solution

  • If I understood you correctly, this would be your way to go:

    C#-Code

    using System;
    
    namespace ConsoleApp2
    {
        public class Program
        {
            private static void Main()
            { 
                double a = -178.125;
                double aBound = -1.875;
                double b = 0.000;
                double bBound = 180.000;
                double inc = 1.875;
    
                // counts from a to aBound (-178.125 to -1.875) and prints the values to console
                Console.WriteLine(a);
                while (a < aBound)
                {
                    a += inc;
                    Console.WriteLine(a);
                }
    
                // counts from b to bBound (0.000 to 180.000) and prints the values to console
                Console.WriteLine("+" + b);
                while (b < bBound)
                {
                    b += inc;
                    Console.WriteLine("+" + b);
                }
    
                Console.Read();
            }   
        }
    }