0

I've run into the floating point issue, and I hoped to attack it using the decimal module. Unfortunately, to my surprise, simply passing the floats to the Decimal constructor does not seem to generically solve the problem. A simple illustration of a broader issue below. Please, can someone explain this? Is converting to strings before passing to the Decimal the only way forward?

Please note, my question is not about this specific division of the two numbers, it's a general issue/concept that I'm trying to address.

enter image description here

Simon Righley
  • 4,538
  • 6
  • 26
  • 33
  • 1
    In your case, I think the safest way is using strings (e.g. `Decimal('0.2125')`). [This "quick start tutorial"](https://docs.python.org/3/library/decimal.html#quick-start-tutorial) in the Python docs could be useful – aaossa Feb 10 '22 at 18:50
  • 1
    And [please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Feb 10 '22 at 18:50

0 Answers0