Why does System.MidpointRounding.AwayFromZero not round up in this instance?

后端 未结 5 1360
星月不相逢
星月不相逢 2020-12-03 06:55

In .NET, why does System.Math.Round(1.035, 2, MidpointRounding.AwayFromZero) yield 1.03 instead of 1.04? I feel like the answer to my question lies in the sect

5条回答
  •  Happy的楠姐
    2020-12-03 07:14

    I'ts because the BINARY representation of 1.035 closer to 1.03 than 1.04

    For better results do it this way -

    decimal result = decimal.Round(1.035m, 2, MidpointRounding.AwayFromZero);
    

提交回复
热议问题