0

In below code snippet I am expecting 13.70 in both cases, but I am getting 13.69 in first and 13.70 in second.

var d1 = Decimal.Round(13.694999999999999999999999998M, 2, MidpointRounding.AwayFromZero)
var d2 = Decimal.Round(13.695000M, 2, MidpointRounding.AwayFromZero);
Emond
  • 50,210
  • 11
  • 84
  • 115
azhahes.s
  • 35
  • 1
  • 5

1 Answers1

5
var d1 = Decimal.Round(13.694999999999999999999999998M, 2, MidpointRounding.AwayFromZero);

13.694999999999 is less than 13.695 so it will round to 13.69

MidpointRounding.AwayFromZero has no effect in this case.

var d2 = Decimal.Round(13.695000M, 2, MidpointRounding.AwayFromZero);

13.695 is exactly in between 13.69 and 13.70 so the rounding depends on MidpointRounding.AwayFromZero which results in 13.70 (which is away from 0)

It is wrong to expect the rounding to round less significant digits and use those values to round higher significant digits. E.g.:

Round 13.694999999999 to 13.695 and then round 13.695 to 13.70

Emond
  • 50,210
  • 11
  • 84
  • 115