Question about floating point
On Wed, Aug 29, 2018 at 12:47 AM, Frank Millman <frank at chagford.com> wrote:
> They were interesting, but actually did not answer the question that I
> forgot to ask!
> The reason for my query is this. I am assisting someone with an application
> involving monetary values. I have been trying to explain why they should use
> the decimal module. They have had a counter-argument from someone else who
> says they should just use the rounding technique in my third example above.
> My gut-feel says that they are wrong, but I don't have the knowledge to
> prove it. I think a lot depends on what they are going to do with this value
> down the line - maybe it is 'safe enough' for their purposes, so I don't
> want to overstate my case. Are there edge cases where that rounding method
> could fail?
For virtually all situations, the correct solution is neither float
nor decimal; the correct way to handle money is an integer, storing a
number of cents, yen, pence, or whatever your smallest unit is. In the
rare situations where you need fractional cents, you probably need to
use decimal.Decimal(), but if you're doing that sort of thing (eg
forex), you'll know what your requirements are far better than I do.
Everywhere else, integers make way better sense than anything else.