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

后端 未结 5 1357
星月不相逢
星月不相逢 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条回答
  •  执笔经年
    2020-12-03 07:13

    I believe the example you're referring to is a different issue; as far as I understand they're saying that 0.1 isn't stored, in float, as exactly 0.1, it's actually slightly off because of how floats are stored in binary. As such let's suppose it actually looks more like 0.0999999999999 (or similar), something very, very slightly less than 0.1 - so slightly that it doesn't tend to make much difference. Well, no, they're saying: one noticeable difference would be that adding this to your number and rounding would actually appear to go the wrong way because even though the numbers are extremely close it's still considered "less than" the .5 for rounding.

    If I misunderstood that page, I hope somebody corrects me :)

    I don't see how it relates to your call, though, because you're being more explicit. Perhaps it's just storing your number in a similar fashion.

提交回复
热议问题