Home > Software design >  How to print 0.1 to 0.100 with increment c#
How to print 0.1 to 0.100 with increment c#

Time:10-19

I want to print decimal numbers from 0.1 to 0.100 In a loop, how can you print with increment?

for(decimal i=0.1;i<=0.100;i  )
{
   console.writeline(i);
}

My decimal value getting change from 0.10 to 1.0.

want to print 0.1 , 0.2, 0.3... 0.10,0.11, 0.12...0.100

CodePudding user response:

Well the root problem is that 0.1 and 0.100 are the same number. Also that i is syntactic sugar, it is equal to i = i 1. It seems like you don't want the numbers in the mathematical sense, but in the "version" sense (like version 1.1 is older than 1.10), and in that sense each number (before or after the dot) is treated as a natural number, i.e positive integers.

In your particular case, the positive integers between (and including) 1 and 100, and then just print them out in a fancy format, which is easy enough:

for (int i = 0; i <= 100; i  )
{
    Console.WriteLine($"0.{i}");
}

P.S the $ before the string signals to C# that this is a interpolated string, they're really cool and useful.

CodePudding user response:

Answering strictly to your question without other assumptions. Just to print the numbers:

for(int i=1;i<=100;i  )
    {
       Console.WriteLine("0."  (i<10 ? i "0": i.ToString()));
    }
}

CodePudding user response:

    for (int i = 1; i <= 100; i  )
    {
        Console.WriteLine ("0.{0}",i);
    }
  •  Tags:  
  • c#
  • Related