C# Maths gives wrong results!

后端 未结 4 904
迷失自我
迷失自我 2020-12-12 01:53

I understand the principle behind this problem but it\'s giving me a headache to think that this is going on throughout my application and I need to find as solution.

<
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 01:59

    Instead of using Round to round the number, you could use some function you write yourself which uses a small epsilon when rounding to allow for the error. That's the answer you want.

    The answer you don't want, but I'm going to give anyway, is that if you want precision, and since you're dealing with money judging by your example you probably do, you should not be using binary floating point maths. Binary floating point is inherently inaccurate and some numbers just can't be represented correctly. Using Decimal, which does base-10 floating point, would be a much better approach everywhere and will avoid you making costly mistakes with your doubles.

提交回复
热议问题