0

Why does below code returns 1299.49 ? According to msdn documentation it should give 1299.5.

Console.WriteLine(Math.Round( 1299.492, 2, MidpointRounding.AwayFromZero))
Pritam
  • 1,288
  • 5
  • 23
  • 40
  • 5
    What makes you think it should give 1299.5? In what way is 1299.492 the midpoint of 1299.49 and 1299.50? – Jon Skeet Sep 30 '15 at 14:39
  • Thanks for your reply. But I am still confused here as 1299.492 is somewhere between 1299.49 and 1299.50. So after two decimal rounding it should give me 1299.50 is this correct ? @JonSkeet – Pritam Sep 30 '15 at 15:05
  • 3
    Nope. I suggest you read the docs for `Math.Round` more carefully. In particular: "A parameter specifies how to round the value if it is midway between two numbers." "Somewhere between" isn't the same as "midway between". – Jon Skeet Sep 30 '15 at 15:07

2 Answers2

3

You are rounding to 2 decimal places, hence why 1299.49 is returned.

If you want it to be 1299.5, round to 1 decimal place.

Console.WriteLine(Math.Round( 1299.492, 1, MidpointRounding.AwayFromZero))

From the documentation about AwayFromZero:

When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

https://msdn.microsoft.com/en-us/library/system.midpointrounding(v=vs.110).aspx

user1666620
  • 4,800
  • 18
  • 27
  • Thanks for your reply. But I am still confused here as 1299.492 is somewhere between 1299.49 and 1299.50. So after two decimal rounding it should give me 1299.50 is this correct ? – Pritam Sep 30 '15 at 15:04
  • 1
    @Pritam 1299.492 is *somewhere* between those two numbers, but it is not HALF WAY between those two numbers. It will go to 1299.5 if the value is 1299.495 or more. – user1666620 Sep 30 '15 at 15:06
  • 3
    It's worth noting that if you call it with 1299.495, you'll still get 1299.49, because the literal 1299.495 actually ends up as 1299.4949999999998908606357872486114501953125. – Jon Skeet Sep 30 '15 at 15:09
2

You may be confused as to how this overload works. According to MSDN on MidpointRounding:

When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

In your case, 1299.492 is not halfway between 1229.49 and 1299.50, so MidpointRounding.AwayFromZero doesn't even apply.


It looks like what you're actually trying to do is round up to the nearest 2 decimal places. In that case, you want something like this answer:

public static double RoundUp(double input, int places)
{
    double multiplier = Math.Pow(10, Convert.ToDouble(places));
    return Math.Ceiling(input * multiplier) / multiplier;
}

This rounds up to the specified decimal places by multiplying by 10^places (100 if you need 2 places), calling Math.Ceiling, and then dividing.

This works:

Console.WriteLine(RoundUp(1299.492, 2)); // 1299.5
Community
  • 1
  • 1
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
  • Thanks for your reply. But I am still confused here as 1299.492 is somewhere between 1299.49 and 1299.50. So after two decimal rounding it should give me 1299.50 is this correct ? – Pritam Sep 30 '15 at 15:03