Why is using a NON-decimal data type bad for money?

前端 未结 5 1629
执念已碎
执念已碎 2021-02-08 23:13

tl;dr: What\'s wrong with my Cur (currency) structure?

tl;dr 2: Read the rest of the question please, before giving an

5条回答
  •  太阳男子
    2021-02-08 23:44

    Usually monetary calculations require exact results, not just accurate results. float and double types cannot accurately represent the whole range of base 10 real numbers. For instance, 0.1 cannot be represented by a floating-point variable. What will be stored is the nearest representable value, which may be a number such as 0.0999999999999999996. Try it out for yourself by unit testing your struct - for example, attempt 2.00 - 1.10.

提交回复
热议问题