0
static void Main(string[] args)
    {
        for (double i = 4; i <= 6; i += 0.1)
        {
            Console.WriteLine(i);
        }
        Console.ReadLine();
    }

Output is:

4

4.1

4.2

4.3

4.4

4.5

4.6

4.7

4.8

4.9

5

5.1

5.2

5.3

5.4

5.49999999999999

5.59999999999999

5.69999999999999

5.79999999999999

5.89999999999999

5.99999999999999

  • 1
    `0.1` is not representable exactly in binary floating point. – Bathsheba Jul 07 '17 at 11:53
  • 1
    Replace double with decimal – Tim Schmelter Jul 07 '17 at 11:55
  • 1
    or loop with an integral type and scale to the `double` at the point of use. – Bathsheba Jul 07 '17 at 11:55
  • 1
    Using `decimal` isn't always the best solution. Like `double`, `decimal` is only an approximation. There are numbers that `decimal` can't represent either. It's worth understand why one approximation is better than the other. `decimal` is a base 10 floating point number and will represent `0.1` better than `double` does. – Enigmativity Jul 07 '17 at 12:38

0 Answers0