Dave

Dave

  • NA
  • 161
  • 0

Math.Round()

Apr 29 2008 5:02 AM
Hi all,

I knocked up a quick program to divide marks by 2. I wanted to round to 2 decimal places. My code is:

static void Main(string[] args)
        {
            double a, b;

            Console.Write("Enter mark:");

            a = double.Parse(Console.ReadLine());

            while (a != -1)
            {               
                b = a / 2.0;

                b = Math.Round(b, 2, MidpointRounding.AwayFromZero);

                Console.WriteLine(b + "\n");
                Console.WriteLine("Enter mark:");
                a = double.Parse(Console.ReadLine());               
            }

            Console.ReadLine();
        }


The thing that I do not get - when I enter 36.05, it outputs 18.02. Now, 36.05 / 2.0 equals 18.025. So my question is, why doesn't Math.Round successfully round that up to 18.03?

There's no hassle or rush on this. It is just a point of curiosity for me. I don't like it when I cannot figure out why I don't get an expected result.

Cheers

Answers (2)